show a zero if group by returns no value

前端 未结 1 1342
误落风尘
误落风尘 2021-01-26 02:10

I am trying to get the amount of users signed up in the past 7 days to display on a chart and it would be nice if mysql returned 0 instead of no row. Currently it just returns a

相关标签:
1条回答
  • 2021-01-26 02:18

    Try:

    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)
    
    0 讨论(0)
提交回复
热议问题