我想用当前日期更改2016-11-20
SELECT services, loket,
SUM( CASE WHEN STATUS IN ('7','4') THEN 1 ELSE 0 END ) AS Total_queue,
SUM( CASE WHEN STATUS = '4' THEN 1 ELSE 0 END ) AS balance_queue, SUM( CASE WHEN STATUS = '7' THEN 1 ELSE 0 END ) AS nomer_queue
FROM tabelqueue
WHERE STATUS IN ('7','4')
AND get_ticket BETWEEN '2016-11-21 00:00:01' AND '2016-11-21 23:59:59'
GROUP BY services
答案 0 :(得分:1)
将条件写为:
WHERE STATUS IN ('7', '4') AND
get_ticket >= CURDATE() AND get_ticket < DATE_ADD(CURDATE(), interval 1 day)
您的原始状况应该写成:
WHERE STATUS IN ('7', '4') AND
get_ticket >= DATE('2016-11-21') AND
get_ticket < DATE_ADD(DATE('2016-11-21'), interval 1 day)
注意:
DATE(get_ticket) = CURDATE()
。这很容易阅读,但查询无法利用get_ticket
上的索引(如果可用且适当的话)。STATUS
是数字,请不要在常量周围使用引号。引用的数字对于人和优化者都是误导。答案 1 :(得分:0)
使用GETDATE()函数;例如:
WHERE STATUS IN ('7','4')
AND get_ticket BETWEEN GETDATE() AND '2016-11-21 23:59:59'
GROUP BY services
这给出了当前的日期和时间;如果你希望它今天午夜开始,你可以使用:
CONVERT(VARCHAR(12), GETDATE(), 102)