我想要通過計算在同一天創建了多少個條目來可視化我的條目。按日期分組,即使日期沒有條目
SELECT dayname(created_at), count(*) FROM logs
group by day(created_at)
ORDER BY created_at desc
LIMIT 7
所以我得到的是這樣的:
Thursday 4
Wednesday 12
Monday 4
Sunday 1
Saturday 20
Friday 23
Thursday 10
但我也想有周二在那裏與0,所以我有一個星期。
有沒有辦法做到這一點與完整的MySQL或我需要更新結果之前,我可以把它給圖表?
編輯:
這是最終的查詢:
SELECT
DAYNAME(date_add(NOW(), interval days.id day)) AS day,
count(logs.id) AS amount
FROM days LEFT OUTER JOIN
(SELECT *
FROM logs
WHERE TIMESTAMPDIFF(DAY,DATE(created_at),now()) < 7) logs
on datediff(created_at, NOW()) = days.id
GROUP BY days.id
ORDER BY days.id desc;
表天包括從0到-6
這就是它!我將硬編碼日期更改爲NOW(),它工作正常:) – mimo
@mimo酷。如果你實際上不想在報告中覆蓋8天,希望你拿出-7。 – shawnt00