与“00:00:00”冲突为END_TIME

时间:2014-03-06 11:07:29

标签: sql codeigniter time between

我有这个问题:

SELECT `s`.`time` , SUM( s.love ) AS total_love, SUM( s.sad ) AS total_sad, SUM( s.angry ) AS total_angry, SUM( s.happy ) AS total_happy
FROM (`employee_workshift` AS e)
JOIN `workshift` AS w ON `e`.`workshift_uuid` = `w`.`uuid`
JOIN `shift_summary` AS s ON `w`.`uuid` = `s`.`workshift_uuid`
WHERE `s`.`location_uuid` = '81956feb-3fd7-0e84-e9fe-b640434dfad0'
AND `e`.`employee_uuid` = '3866a979-bc5e-56cb-cede-863afc47b8b5'
AND `s`.`workshift_uuid` = '8c9dbd85-18a3-6ca9-e3f3-06eb602b6f38'
AND `s`.`time` >= CAST( '18:00:00' AS TIME )
AND `s`.`time` <= CAST( '00:00:00' AS TIME )
AND `s`.`date` LIKE '%2014-03%'

我的问题是它返回&#34; NULL&#34;但是当我改变了我的'end_time&#39;到&#34; 23:59:59&#34;,它返回了正确的数据。我已经有了一个想法,可以拉开两个小时的开始时间&#39;和&#39; end_time&#39;然后将它插入循环中以获取它们之间的所有内容。

$time_start = 15;
$time_end = 03;

So it should produce: 15,16,17,18,19,20,21,22,23,00,01,02,03

然后我将它们全部进行比较。但这需要花费很多精力和努力,而不仅仅是简单地使用&#34; BETWEEN&#34;。或者我应该使用&#34; in_array&#34;?你遇到过这个吗?我希望有人可以提供帮助。感谢。

2 个答案:

答案 0 :(得分:0)

<00> 19:00肯定比00:00更大 - 所以你的方法不行。

尝试使用完整时间戳(包括日期)来获取所需的所有数据。

答案 1 :(得分:0)

尝试使用此查询。我不知道您的数据结构,因此请在INNER JOINs表之间检查s1。连接必须是一行到一行 - 仅在日期中有所不同。 s1行的日期必须早于1天,而不是s表行。

 SELECT s.time , SUM( s.love ) AS total_love, SUM( s.sad ) AS total_sad, SUM( s.angry ) AS total_angry, SUM( s.happy ) AS total_happy
    FROM (employee_workshift AS e)
    JOIN workshift AS w ON e.workshift_uuid = w.uuid
    JOIN shift_summary AS s ON w.uuid = s.workshift_uuid
    JOIN shift_summary AS s1 ON (w.uuid = s.workshift_uuid AND CAST(s.date as DATE)=CAST(s1.date as DATE)+1)
    WHERE s.location_uuid = '81956feb-3fd7-0e84-e9fe-b640434dfad0'
    AND e.employee_uuid = '3866a979-bc5e-56cb-cede-863afc47b8b5'
    AND s.workshift_uuid = '8c9dbd85-18a3-6ca9-e3f3-06eb602b6f38'
    AND s1.time >= CAST( '18:00:00' AS TIME )
    AND s.time <= CAST( '00:00:00' AS TIME )
    AND s.date LIKE '%2014-03%'