具有条件的字段的MySQL SUM,IF(SUM(bill_1)IS NULL,'99',SUM(bill_1))AS tot_bill_1

时间:2015-02-15 06:22:59

标签: mysql if-statement join sum conditional-statements

我想要SUM(tot_bill_1 + tot_bill_2)AS总数,但它不起作用

SELECT *, 
IF(SUM(bill_1) IS NULL, '99', SUM(bill_1)) AS tot_bill_1, 
IF(SUM(bill_2) IS NULL, '10', SUM(bill_2)) AS tot_bill_2, 
SUM(tot_bill_1+tot_bill_2) AS total
FROM billing

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用子查询执行此操作:

SELECT sum_bill1,
       sum_bill2,
       (sum_bill1 + sum_bill2) AS total
FROM
  (SELECT COALESCE(sum(bill1), 99) AS sum_bill1,
          COALESCE(sum(bill2), 10) AS sum_bill2
   FROM billings) t