我在过去10天内使用以下内容来计算新用户:
SELECT days.day, count(u.user_id)
FROM
(select curdate() as day
union select curdate() - interval 1 day
union select curdate() - interval 2 day
union select curdate() - interval 3 day
union select curdate() - interval 4 day
union select curdate() - interval 5 day
union select curdate() - interval 6 day
union select curdate() - interval 7 day
union select curdate() - interval 8 day
union select curdate() - interval 9 day) days
left join users u
on days.day = DATE(u.dateadded)
group by
days.day
哪个有效,但是应该给出date_format,2014-03-18
- 理想情况下,我希望日期格式为例如'Tues 18th Mar'
这就是我正在尝试但仅返回2行,并且在天列下显示null
和BLOB
SELECT days.day, count(u.user_id)
FROM
(select DATE_FORMAT(curdate(), '%a %D %b') as day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 1 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 2 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 3 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 4 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 5 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 6 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 7 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 8 day
union select DATE_FORMAT(curdate(), '%a %D %b') - interval 9 day) days
left join users u
on days.day = DATE_FORMAT(u.dateadded, '%a %D %b')
group by
days.day
有更好的方法吗?
我正在使用MySQL和PHP
答案 0 :(得分:2)
JOIN
当然必须保持不变。无需在那里使用DATE_FORMAT()
。
SELECT DATE_FORMAT(days.day, '%a %D %b') AS "day", count(u.user_id)
FROM
(select curdate() as day
union select curdate() - interval 1 day
union select curdate() - interval 2 day
union select curdate() - interval 3 day
union select curdate() - interval 4 day
union select curdate() - interval 5 day
union select curdate() - interval 6 day
union select curdate() - interval 7 day
union select curdate() - interval 8 day
union select curdate() - interval 9 day) days
left join users u
on days.day = DATE(u.dateadded)
group by
days.day
答案 1 :(得分:1)
尝试此选项:
SELECT DATE_FORMAT(dateadded, '%a %D %b'), count(1)
FROM users
WHERE dateadded > put_here_your_limit
GROUP BY 1
ORDER BY 1 DESC;
我希望它适合你。
答案 2 :(得分:0)
你的问题是在date_format的结果上使用- interval
而不是它的参数(导致为null,因为在格式化之后,它看起来不像要减去的有效日期 - 只有一个null所有union select
的结果,因为您没有说union all
)。这可行:
select DATE_FORMAT(curdate(), '%a %D %b') as day
union select DATE_FORMAT(curdate() - interval 1 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 2 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 3 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 4 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 5 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 6 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 7 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 8 day , '%a %D %b')
union select DATE_FORMAT(curdate() - interval 9 day , '%a %D %b')
但是因为您对用户的加入只是在例如“Tue 18th Mar”,你今天会增加用户,但也会在其他年份增加用户,例如2008年3月18日星期二。我猜这不是你的意图。