我有一张表,日期明智的数据在那里,即使数据不存在,我怎样才能获得月数据。我试过但我只能获得数据库中的可用数据,但我需要所有月数据,如果没有数据。我试过这个查询
select
count(date) as absentdays,
MONTHNAME(date) as monthname,
MONTH(date) as monthnumber
from `tbl_records`
where FIND_IN_SET(2920,users_absent)
GROUP BY YEAR(date), MONTH(date)
答案 0 :(得分:1)
您可以使用UNION
执行此操作;)
SELECT SUM(absentdays) AS absentdays, `MONTHNAME`, monthnumber
FROM (
SELECT COUNT(DATE) AS absentdays, MONTHNAME(DATE) AS `MONTHNAME`, MONTH(DATE) AS monthnumber
FROM `tbl_records`
WHERE FIND_IN_SET(2920,users_absent)
GROUP BY YEAR(DATE), MONTH(DATE)
UNION SELECT 0 AS absentdays, 'January' AS MONTHNAME, 1 AS monthnumber
UNION SELECT 0 AS absentdays, 'February' AS MONTHNAME, 2 AS monthnumber
UNION SELECT 0 AS absentdays, 'March' AS MONTHNAME, 3 AS monthnumber
UNION SELECT 0 AS absentdays, 'April' AS MONTHNAME, 4 AS monthnumber
UNION SELECT 0 AS absentdays, 'May' AS MONTHNAME, 5 AS monthnumber
UNION SELECT 0 AS absentdays, 'June' AS MONTHNAME, 6 AS monthnumber
UNION SELECT 0 AS absentdays, 'July' AS MONTHNAME, 7 AS monthnumber
UNION SELECT 0 AS absentdays, 'August' AS MONTHNAME, 8 AS monthnumber
UNION SELECT 0 AS absentdays, 'September' AS MONTHNAME, 9 AS monthnumber
UNION SELECT 0 AS absentdays, 'October' AS MONTHNAME, 10 AS monthnumber
UNION SELECT 0 AS absentdays, 'November' AS MONTHNAME, 11 AS monthnumber
UNION SELECT 0 AS absentdays, 'December' AS MONTHNAME, 12 AS monthnumber
) t
GROUP BY `MONTHNAME`, monthnumber