MYSQL的time型別值進行統計

2021-08-25 06:13:01 字數 4055 閱讀 4780

1.實現功能場景:統計一天中工作時間

2.建表:

create table `loadmovement` (

`uuid` varchar (96),

`criminalid` varchar (96),

`movementdate` datetime ,

`exittime` varchar (60),

`returntime` varchar (60),

`nextdateindicator` double ,

`movedays` double ,

`modifytime` datetime ,

`createtime` datetime ,

`valid` double

); insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c616bf0129c6178c3a0003','402880e928c7ca190128c7dd863d0005','2010-07-12 17:58:38','11:15:00','00:30:00','1','2','2010-07-12 19:05:44','2010-07-12 17:58:38','0');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c9870e280003','402880e928c7ca190128c7dd863d0005','2010-07-13 09:59:18','01:00:00','00:30:00','1','2','2010-07-13 10:14:02','2010-07-13 09:59:18','0');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c9870e280004','402880e928c7ca190128c7dd863d0005','2010-07-13 09:59:18','11:30:00','01:00:00','1','2','2010-07-13 10:04:12','2010-07-13 09:59:18','0');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c9870e280005','402880e928c7ca190128c7dd863d0005','2010-07-13 09:59:18','11:00:00','00:00:00','1','1','2010-07-13 10:12:30','2010-07-13 09:59:18','0');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c9870e280006','402880e928c7ca190128c7dd863d0005','2010-07-13 09:59:18','11:45:00','01:00:00','1','3','2010-07-13 10:09:27','2010-07-13 09:59:18','0');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c99b1b56000d','402880e928c7ca190128c7dd863d0005','2010-07-13 10:21:12','11:30:00','00:45:00','1','2','2010-07-13 10:22:08','2010-07-13 10:21:12','0');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c9a42378000f','402880e928c7ca190128c7dd863d0005','2010-07-13 10:31:04','11:30:00','00:15:00','1','4','2010-07-13 10:31:04','2010-07-13 10:31:04','1');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c9a423780010','402880e928c7ca190128c7dd863d0005','2010-07-13 10:31:04','11:30:00','01:00:00','1','2','2010-07-13 10:31:04','2010-07-13 10:31:04','1');

insert into `loadmovement` (`uuid`, `criminalid`, `movementdate`, `exittime`, `returntime`, `nextdateindicator`, `movedays`, `modifytime`, `createtime`, `valid`) values('402880e929c97a710129c9a423780011','402880e928c7ca190128c7dd863d0005','2010-07-13 10:31:04','11:00:00','00:45:00','1','1','2010-07-13 10:31:04','2010-07-13 10:31:04','1');

3.查詢:

select movedays,  time_format(

sec_to_time(sum(case when nextdateindicator=1

then

time_to_sec(returntime)-time_to_sec(exittime)+time_to_sec('24:00:00')

else

time_to_sec(returntime)-time_to_sec(exittime)

end)),'%t') as calctime, returntime,exittime

from loadmovement where criminalid='402880e928c7ca190128c7dd863d0005'

group by movedays

4.說明time_format非常重要,本人就是缺少了這個函式,致使在前端的顯示為(1970/1/1),若要顯示為(hh:mm:ss)格式,須加上此函式

Mysql中對字串型別的字段進行數字值排序

order by meta value 那麼按得分排序得到的結果可能是 1 1011 1231234225 2533由於是按字母順序排列,所以123排在了2的前面,顯然不符合我們的要求,那麼怎樣才能按照我們預想的數字順序排序呢?下面介紹兩種方法。一 採用mysql的cast函式,轉換字段格式 這裡我...

MySQL欄位型別及長度值

一 數字型別 型別 範圍 說明 n 1 255 個字元 binary 分辨大小寫 固定長度 std name cahr 32 not null n 1 255 個字元 binary 分辨大小寫 可變長度 std address varchar 256 tinyblob 最大長度255個字元 2 8 ...

MySQL中Int型別的長度值問題

mysql在建表的時候int型別後的長度代表什麼?是該列允許儲存值的最大寬度嗎?為什麼我設定成int 1 也一樣能存10,100,1000呢.當時我雖然知道int 1 這個長度1並不代表允許儲存的寬度,但卻沒有乙個合理的解釋.或者說對這個長度也沒有真正的研究過到底代表什麼,平時都用int 11 也不...