从日期范围中选择特定的一天数据

时间:2017-07-13 10:19:22

标签: mysql

我有这样的脚本从一天'xxxx-xx-xx 00:00:00'开始到今天结束'xxxx-xx-xx 23:59:59'查询userCount。

USE db;
SELECT user
FROM db.user AS user
WHERE
user.payTime BETWEEN '2017-07-12 00:00:00' AND '2017-07-12 23:59:59'
GROUP BY user.username

那么现在如何在2017-06-012017-07-12的那一天内查询用户的付款时间?

我想到的第一件事是宣布日期范围:

SET CURRDATE = '2017-06-01';
SET ENDDATE = '2017-07-12';

并将脚本放入如下的循环中:

WHILE CURRDATE < ENDDATE DO
   USE db;
   .......
   WHERE
   user.payTime BETWEEN 'CURRDATE 00:00:00' AND 'CURRDATE 23:59:59'
   .......
   SET CURRDATE = DATE_ADD(CURRDATE, INTERVAL 1 DAY)
END WHILE;

1 个答案:

答案 0 :(得分:0)

试试这个 -

SELECT user, user.payTime
FROM db.user AS user
WHERE user.payTime BETWEEN '2017-06-01 00:00:00' AND '2017-07-12 23:59:59'
GROUP BY CAST(user.payTime as DATE)