Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all rows except one in MySQL

Tags:

mysql

I'm trying to use a SELECT statement and fetch all rows from a table except for one which has an id of 4 - is there a simple way to do this?

like image 382
woninana Avatar asked Jun 07 '11 04:06

woninana


People also ask

How do I select all rows except one row in SQL?

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 exclude a record in MySQL?

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.


3 Answers

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.

In which case you would have:

SELECT * FROM table
WHERE id NOT IN (SELECT id FROM exempt_items_table);
like image 152
Tim Avatar answered Oct 19 '22 07:10

Tim


select * from table where some_id != 4
like image 7
Christopher Armstrong Avatar answered Oct 19 '22 09:10

Christopher Armstrong


select * from <table name> where <column - name> != <value>;
like image 4
Stuti Avatar answered Oct 19 '22 09:10

Stuti