woninana
woninana

Reputation: 3481

Select all rows except one in 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?

Upvotes: 18

Views: 51280

Answers (3)

Stuti
Stuti

Reputation: 1638

select * from <table name> where <column - name> != <value>;

Upvotes: 4

Tim
Tim

Reputation: 5822

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);

Upvotes: 42

Christopher Armstrong
Christopher Armstrong

Reputation: 7953

select * from table where some_id != 4

Upvotes: 8

Related Questions