0

我试图让过去 7 天内注册的用户数量显示在图表上,如果 mysql 返回 0 而不是没有行,那就太好了。目前它只为每一天返回一行,它有一个值

SELECT   date(created_at), 
         count(id) 
FROM     user_accts 
WHERE    date(created_at) < NOW() 
AND      date(created_at) > DATE_SUB(NOW(), INTERVAL 7 DAY) 
GROUP BY date(created_at);
4

1 回答 1

3

尝试:

SELECT DATE_SUB(CURDATE(), INTERVAL i DAY) date_created_at, 
       count(id) 
FROM (SELECT 1 i UNION ALL 
      SELECT 2 UNION ALL 
      SELECT 3 UNION ALL 
      SELECT 4 UNION ALL 
      SELECT 5 UNION ALL 
      SELECT 6 UNION ALL 
      SELECT 7) i
LEFT JOIN user_accts ON  date(created_at) = DATE_SUB(CURDATE(), INTERVAL i DAY)
                   /*AND owner_id = '131'*/
GROUP BY DATE_SUB(CURDATE(), INTERVAL i DAY)
于 2013-02-08T12:07:05.493 回答