我有一個基本的SQL查詢:SQL技巧按月計算行數?
SELECT MONTH(created_at), COUNT(id) as total FROM `clients` GROUP BY MONTH(created_at)
它返回我按月等作爲細分電子郵件數據:
MONTH(created_at) | total
09 1
10 2
如何填寫剩下的幾個月爲零?所以,結果,我需要得到所有月份:
MONTH(created_at) | total
09 1
.. 2
12 5
我試着這樣說:
SELECT months.id, COUNT(clients.id) as total FROM `months` LEFT JOIN `clients` ON MONTH(created_at) = months.id GROUP BY MONTH(created_at)
它看起來可怕,性能如何呢? – OPV
請看我的編輯 – OPV