我想知道是否可以进行如下查询:
SELECT COUNT(DISTINCT `User`) FROM `test_table` WHERE `createdDate` >= now() - INTERVAL 1 DAY AS Day
&&
SELECT COUNT(DISTINCT `User`) FROM `test_table` WHERE `createdDate` >= now() - INTERVAL 7 DAY AS Week
&&
SELECT COUNT(DISTINCT `User`) FROM `test_table` WHERE `createdDate` >= now() - INTERVAL 30 DAY AS Month
理论上,查询将返回过去一个月/周/天创建的用户数量。
答案 0 :(得分:0)
只需使用DATE_SUB(NOW(),INTERVAL 1 DAY)
SELECT COUNT(DISTINCT `User`) as dailyUser FROM `test_table` WHERE `createdDate` >= DATE_SUB(NOW(), INTERVAL 1 DAY)
SELECT COUNT(DISTINCT `User`) as weeklyUser FROM `test_table` WHERE `createdDate` >= DATE_SUB(NOW(), INTERVAL 1 WEEK)
SELECT COUNT(DISTINCT `User`) as monthlyUser FROM `test_table` WHERE `createdDate` >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
答案 1 :(得分:0)
SELECT
IFNULL(SUM(CASE WHEN `createdDate` >= CURDATE() - INTERVAL 1 DAY THEN 1 ELSE 0 END),'0') AS 'LastDay',
IFNULL(SUM(CASE WHEN `createdDate` >= CURDATE() - INTERVAL 7 DAY THEN 1 ELSE 0 END),'0') AS 'LastSevenDays',
IFNULL(SUM(CASE WHEN `createdDate` >= CURDATE() - INTERVAL 30 DAY THEN 1 ELSE 0 END),'0') AS 'LastThirtyDays'
FROM
test_table
WHERE `createdDate` >= CURDATE() - INTERVAL 30 DAY;