Mysql group by month,如果为0,则为null值

时间:2017-03-01 22:19:14

标签: mysql

真的很简单(在这里找不到例子)。基本上我想为缺少的月份添加空值。

目前我有

|month| total |
----------------
   2  | 2454.34
   3  | 1254.34

我想要的地方

|month| total |
----------------
   1  |    0
   2  | 2454.34
   3  | 1254.34
   4  |    0
   5  |    0
   6  |    0

等等。

到目前为止我的查询

SELECT MONTH(bookings.booking) as month, SUM(bookings.tendered) as total
FROM bookings 
INNER JOIN salons ON salons.id = bookings.salon_id
WHERE bookings.paid = 1
AND YEAR(bookings.booking) = 2017
GROUP BY MONTH(bookings.booking)
ORDER BY MONTH(bookings.booking);

我确实尝试过以下但似乎没有按照我想要的方式工作?请原谅上述示例中的不同格式

SUM(IF(MONTH(bookings.booking) = 'Jan', bookings.tendered, 0)) AS 'Jan',
        SUM(IF(MONTH(bookings.booking) = 'Feb', bookings.tendered, 0)) AS 'Feb',
        SUM(IF(MONTH(bookings.booking) = 'Mar', bookings.tendered, 0)) AS 'Mar',
        SUM(IF(MONTH(bookings.booking) = 'Apr', bookings.tendered, 0)) AS 'Apr',
        SUM(IF(MONTH(bookings.booking) = 'May', bookings.tendered, 0)) AS 'May',
        SUM(IF(MONTH(bookings.booking) = 'Jun', bookings.tendered, 0)) AS 'Jun',
        SUM(IF(MONTH(bookings.booking) = 'Jul', bookings.tendered, 0)) AS 'Jul',
        SUM(IF(MONTH(bookings.booking) = 'Aug', bookings.tendered, 0)) AS 'Aug',
        SUM(IF(MONTH(bookings.booking) = 'Sep', bookings.tendered, 0)) AS 'Sep',
        SUM(IF(MONTH(bookings.booking) = 'Oct', bookings.tendered, 0)) AS 'Oct',
        SUM(IF(MONTH(bookings.booking) = 'Nov', bookings.tendered, 0)) AS 'Nov',
        SUM(IF(MONTH(bookings.booking) = 'Dec', bookings.tendered, 0)) AS 'Dec',
        SUM(tendered) AS total

已更新

select `a`.`month` as `month_int`, IFNULL(SUM(bookings.tendered), 0) as total from `bookings` right join (
SELECT 1 as month
UNION SELECT 2 as month
UNION SELECT 3 as month
UNION SELECT 4 as month
UNION SELECT 5 as month
UNION SELECT 6 as month
UNION SELECT 7 as month
UNION SELECT 8 as month
UNION SELECT 9 as month
UNION SELECT 10 as month
UNION SELECT 11 as month
UNION SELECT 12 as month\n
) a  on `a`.`month` = MONTH(bookings.booking) where `bookings`.`paid` = ? and date(`bookings`.`booking`) > ? and `salon_id` in (?, ?, ?) group by `a`.`month`

2 个答案:

答案 0 :(得分:1)

您可以在线查看此示例:http://sqlfiddle.com/#!9/8bbf0/1

SELECT
  idMonth,
  MONTHNAME(STR_TO_DATE(idMonth, '%m')) as m,
  IFNULL(sum(Bookings.price), 0) as total
FROM Bookings
RIGHT JOIN (
  SELECT 1 as idMonth
  UNION SELECT 2 as idMonth
  UNION SELECT 3 as idMonth
  UNION SELECT 4 as idMonth
  UNION SELECT 5 as idMonth
  UNION SELECT 6 as idMonth
  UNION SELECT 7 as idMonth
  UNION SELECT 8 as idMonth
  UNION SELECT 9 as idMonth
  UNION SELECT 10 as idMonth
  UNION SELECT 11 as idMonth
  UNION SELECT 12 as idMonth
) as Month
ON Month.idMonth = month(`date`)
GROUP BY Month.idMonth

答案 1 :(得分:0)

此代码可以使用

SELECT m.MONTH as month, 
CASE 
    WHEN SUM(b.total) > 0 THEN SUM(b.total) 
    ELSE 0 END as total
FROM
(
 SELECT '01' AS
 MONTH
 UNION SELECT '02' AS
 MONTH
 UNION SELECT '03' AS   
 MONTH
 UNION SELECT '04' AS
 MONTH 
 UNION SELECT '05' AS
 MONTH
 UNION SELECT '06' AS
 MONTH
 UNION SELECT '07' AS
 MONTH
 UNION SELECT '08' AS
 MONTH
 UNION SELECT '09' AS
 MONTH
 UNION SELECT '10' AS
 MONTH
 UNION SELECT '11' AS
 MONTH
 UNION SELECT '12' AS
 MONTH
 ) AS m
LEFT JOIN
(SELECT MONTH(b.bookings) AS 'month', SUM(b.tendered) AS 'total' 
 FROM bookings AS b
 INNER JOIN salons AS s 
 ON s.id = b.salon_id
 WHERE b.paid = 1
 AND YEAR(b.booking) = 2017) AS n
ON 
m.MONTH = n.month
GROUP BY 1
ORDER BY 1;