woninana
woninana

Reputation: 3481

Select all rows except based on two fields in MySQL

SELECT * FROM table WHERE id != 4;
SELECT * FROM table WHERE NOT id = 4;
SELECT * FROM table WHERE id <> 4;

I've got this all working but I also have to choose another field (or more fields) to decide what rows are returned.

How can I get this working?

Upvotes: 0

Views: 914

Answers (2)

woninana
woninana

Reputation: 3481

select * from albums where idAlbum!= 4 and idAlbum!= 8 I just solve my problem. Thanks for the help guys!

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882716

If you want to 'deselect' columns where both conditions are true (ID1 is 4 and ID2 is 7), use something like:

select * from TBL where ID1 <> 4 or ID2 <> 7;

ID1  ID2  selected
---  ---  --------
 4    7     no
 4    1     yes
 1    7     yes
 1    1     yes

If you want to 'deselect' columns where either condition is true (ID1 is 4 or ID2 is 7), use something like:

select * from TBL where ID1 <> 4 and ID2 <> 7;

ID1  ID2  selected
---  ---  --------
 4    7     no
 4    1     no
 1    7     no
 1    1     yes

This can be extended to more conditions simply by adding them to the end of the where clause (and changing both/either to all/any in the text).

Upvotes: 1

Related Questions