我在PHP中有一个MySQL查询,我得到所有不同的值,然后得到这些不同值的相关列的总和,如下所示:
SELECT t.`fruits`,sum(coalesce(s.amount,0)),sum(coalesce(s.price,0))
FROM(SELECT DISTINCT `fruits` FROM `accouts`) t
LEFT OUTER JOIN `accounts` s
ON(t.`fruits` = s.`fruits` AND s.`userid` = 1
AND s.`date` BETWEEN "2010-11-01" AND "2015-12-22")
GROUP BY t.`fruits`
如何从此查询中排除某个不同的值?特别是如果"水果"列是空的?因为这会返回如下数据:
Oranges - 23 - 43
Pears - 33 - 55
- 12 - 13
Grapes - 54 - 76
我想排除" fruits"是空的。我怎么能这样做呢?
答案 0 :(得分:1)
试试这个:
SELECT t.`fruits`,sum(coalesce(s.amount,0)),sum(coalesce(s.price,0))
FROM(SELECT DISTINCT `fruits` FROM `accouts`) t
LEFT OUTER JOIN `accounts` s
ON(t.`fruits` = s.`fruits` AND s.`userid` = 1
AND s.`date` BETWEEN "2010-11-01" AND "2015-12-22")
WHERE t.`fruits` != ''
GROUP BY t.`fruits`