SELECT a.weekday, COUNT(sl.id) count FROM ( SELECT DATE_SUB(CURDATE(), INTERVAL 0 DAY) AS date
UNION
SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)
UNION
SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) ) as a
LEFT JOIN SpeakandChat_log as sl on FROM_UNIXTIME(sl.time/1000, '%Y-%m-%d')=a.weekday
GROUP BY a.weekday;
Python 生成SQL语句
# 最近30天
sql = "SELECT a.date, COUNT(sl.id) count FROM ( SELECT DATE_SUB(CURDATE(), INTERVAL 0 DAY) AS date "
for i in range(29):
sql += 'UNION SELECT DATE_SUB(CURDATE(), INTERVAL ' + str(i + 1) + ' DAY)'
sql += " ) as a LEFT JOIN SpeakandChat_log as sl on FROM_UNIXTIME(sl.time/1000, '%Y-%m-%d')=a.date GROUP BY a.date;"