简体   繁体   English

MySQL Query:如何选择没有特定值的行?

[英]MySQL Query: How to select rows that don't have a certain value?

I am having trouble writing a query and I don't even know if it is possible. 我在编写查询时遇到问题,我甚至不知道是否可能。 Take this table for example: 以此表为例:

id   group  active  

1    A      NO  
2    A      YES  
3    A      NO  

4    B      YES  
5    B      NO  

6    C      NO  
7    C      NO  

Table above is just an example. 上表只是一个例子。 In real table there are much more columns the those tree so have that in mind. 在实际表中,这些树有更多的列,所以有这些。 What I need is a way to select only group names that don't have any active row. 我需要的是一种只选择没有任何活动行的组名的方法。 In this case both "A" and "B" groups have at least one row with "active" = "YES" but if you look at C there are no active rows. 在这种情况下,“A”和“B”组至少有一行“active”=“YES”,但如果你看C,则没有活动行。 The only thing I would need as a result is a group column value (in this case "C") not entire row. 作为结果,我唯一需要的是组列值(在这种情况下为“C”)而不是整行。

Is this possible? 这可能吗?

SELECT DISTINCT group FROM table WHERE group NOT IN
    (SELECT DISTINCT group FROM table WHERE active = 'YES')

You first want to get all the groups you wish to exclude, and then use the NOT IN clause to return all the other groups not in that list. 您首先要获取要排除的所有组,然后使用NOT IN子句返回不在该列表中的所有其他组。

SELECT DISTINCT t.group 
FROM table t
WHERE t.group NOT IN 
    (SELECT DISTINCT t.group 
     FROM table t 
     WHERE t.active='YES');

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

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