mysql组返回所有行

mysql组返回所有行

我有个问题。。
假设我有这张桌子。。。

waktu               |   tglTransaksiJual   | nominal   | ongkir    | STATUS    | idUser
---------------------------------------------------------------------------------------
2013-02-01 00:00:00 | 2013-01-29 15:00:00  | 10000     | 10000     | APPROVED  | 1
0000-00-00 00:00:00 | 2013-02-02 15:00:00  | 20000     | 0         | DONE      | 2
0000-00-00 00:00:00 | 2013-02-02 15:00:00  | 10000     | 5000      | DONE      | 2
2013-02-03 00:00:00 | 2013-02-01 15:00:00  | 20000     | 0         | APPROVED  | 3

我用这个查询
SELECT DISTINCT(date(IF(waktu = '0000-00-00 00:00:00', tglTransaksiJual, waktu))) AS thedate, SUM( nominal+ongkir ) AS total
        FROM transaksijual
        WHERE IF(waktu = '0000-00-00 00:00:00', tglTransaksiJual, waktu) BETWEEN '2013-02-01' AND '2013-02-05' AND status LIKE 'DONE%' AND idUser LIKE '2%'
        GROUP BY thedate
        ORDER BY thedate ASC

但我得到的结果是
thedate    | total
-------------------
2013-02-01 | 30000

我想要的就是这样
thedate    | total
------------------
2013-02-01 | 0
2013-02-02 | 35000
2013-02-03 | 0

我该怎么做?有可能吗??
所以结论是。。当没有数据时,我想得到0个值并得到行。。。

最佳答案

删除WHERE条件并将SUM列更新为仅在满足条件时求和:

SUM(IF(status = 'DONE' AND idUser = '2', nominal + ongkir, 0))

关于mysql - mysql组返回所有行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14867852/

10-13 00:50