Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

select

mysql

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. The only thing I would need as a result is a group column value (in this case "C") not entire row.

Is this possible?

like image 800
Srka Avatar asked May 10 '10 18:05

Srka


People also ask

How do I exclude something in MySQL?

Write a single MySQL query to exclude a record and display NULL value. To check records which are NULL, use IS NULL. However, to exclude any of the records, use the NOT IN clause. Use both of them in the same query.

How do I SELECT all rows except one?

You have a few options: SELECT * FROM table WHERE id != 4; SELECT * FROM table WHERE NOT id = 4; SELECT * FROM table WHERE id <> 4; Also, considering perhaps sometime in the future you may want to add/remove id's to this list, perhaps another table listing id's which you don't want selectable would be a good idea.

How do I SELECT only certain rows in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.


1 Answers

SELECT DISTINCT group FROM table WHERE group NOT IN
    (SELECT DISTINCT group FROM table WHERE active = 'YES')
like image 120
danben Avatar answered Oct 09 '22 20:10

danben