选择除MySQL中的一行之外的所有行

时间:2022-09-21 15:59:57

I'm trying to use a select statement to get all the rows from a certain MySQL table except for one which has in id of 4. Is there a simple way to do this?

我尝试使用select语句从某个MySQL表中获取所有的行,除了id为4的表。有没有一种简单的方法?

3 个解决方案

#1


28  

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.

另外,考虑到将来的某个时候您可能想要将id添加/删除到这个列表中,也许您不希望可选的另一个表清单id是个好主意。

In which case you would have:

在这种情况下,你会:

SELECT * FROM table
WHERE id NOT IN (SELECT id FROM exempt_items_table);

#2


5  

select * from table where some_id != 4

#3


2  

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

#1


28  

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.

另外,考虑到将来的某个时候您可能想要将id添加/删除到这个列表中,也许您不希望可选的另一个表清单id是个好主意。

In which case you would have:

在这种情况下,你会:

SELECT * FROM table
WHERE id NOT IN (SELECT id FROM exempt_items_table);

#2


5  

select * from table where some_id != 4

#3


2  

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