赞
踩
一、mysql时间戳转换
select unix_timestamp(); 获取当前时间的时间戳
SELECT FROM_UNIXTIME(1529999834); 时间戳转换为北京时间
要得到正确的时间相减秒值,有以下3种方法:
1、time_to_sec(timediff(t2, t1)),
2、timestampdiff(second, t1, t2),
3、unix_timestamp(t2) -unix_timestamp(t1)
---------------------------------------------------------------------------------------------------------
SELECT AcceptTime,CreateTime, (AcceptTime-CreateTime) sec FROM `req_dlr` WHERE SellOrg="CC经销商88" AND CreateTime>="1527350400" AND CreateTime <= "1529942399"
AND AcceptTime <>'0' AND makeid = '36000000' AND ReqState <> '97';
此时用第3种方法,时间戳直接相减
3、timediff(time1,time2),两个时间相减,返回time差值,如图
注意:timediff(time1,time2) 函数的两个参数类型必须相同。
3.1. MySQL (时间和秒互相转换):time_to_sec(time), sec_to_time(seconds)
select time_to_sec('01:00:05'); -- 3605 转换为秒
select sec_to_time(3605); -- '01:00:05'
3.2、
1、req_dlr表中AcceptTime,CreateTime两个时间戳转换为北京时间,然后只取时间
2、把取到的时间转换为秒,相减后求和
SELECT
sum(time_to_sec(M.A)- TIME_TO_SEC(M.C))
FROM
(
SELECT
date_format(
FROM_UNIXTIME(AcceptTime),
'%H:%i:%S'
) A,
date_format(
FROM_UNIXTIME(CreateTime),
'%H:%i:%S'
) C
FROM
`req_dlr`
WHERE
CreateTime >= "1527350400"
AND CreateTime <= "1529942399"
AND AcceptTime <> '0'
AND makeid = '36000000'
AND ReqState <> '97'
) AS M
WHERE
M.A <= '19:00:00'
AND M.A >= '09:00:00';
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。