要排除记录,请使用MySQL NOT IN()
。让我们首先创建一个表-
mysql> create table DemoTable ( Id int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(1); mysql> insert into DemoTable values(2); mysql> insert into DemoTable values(3); mysql> insert into DemoTable values(4); mysql> insert into DemoTable values(5); mysql> insert into DemoTable values(6);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------+ | Id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +------+ 6 rows in set (0.00 sec)
以下是排除记录的查询-
mysql> select *from DemoTable where Id NOT IN(1,4,6,3);
这将产生以下输出-
+------+ | Id | +------+ | 2 | | 5 | +------+ 2 rows in set (0.00 sec)