您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

即使没有对应的mysql结果也如何计数?

即使没有对应的mysql结果也如何计数?

您应该将此表与1..12表保持联接。像这样的东西:

SELECT  Months.id AS `month` ,
COUNT(`reports`.date_lm) AS `count`
FROM 
(
  SELECT 1 as ID UNION SELECT 2 as ID UNION  SELECT 3 as ID UNION SELECT 4 as ID 
  UNION  
  SELECT 5 as ID UNION SELECT 6 as ID UNION SELECT 7 as ID UNION SELECT 8 as ID 
  UNION  
  SELECT 9 as ID UNION SELECT 10 as ID UNION SELECT 11 as ID UNION SELECT 12 as ID
) as Months
LEFT JOIN `reports` on Months.id=month(`reports`.date_lm)
                       AND 
                       (status = 'submitted') 
                       AND (date_lm > 2012-08)
GROUP BY Months.id 
ORDER BY Months.id ASC

SQL Fiddle演示

MySQL 2022/1/1 18:48:40 有296人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶