简体   繁体   English

如何在 MySQL 中选择除一个之外的所有不同值?

[英]How to select all distinct values except one in MySQL?

I have a MySQL query in PHP where I am getting all distinct values and then getting the sums of associated columns of these distinct values like this:我在 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`

How can I exclude a certain distinct value from this query?如何从此查询中排除某个不同的值? Specifically if the "fruits" column is empty?特别是“水果”列是否为空? Because this returns data like this:因为这会返回这样的数据:

Oranges - 23 - 43
Pears   - 33 - 55
        - 12 - 13
Grapes  - 54 - 76

I want to exclude the distinct row where "fruits" is empty.我想排除“水果”为空的不同行。 How can I go about doing this?我该怎么做呢?

Try this:尝试这个:

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`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM