从列表中排除一些 ID 记录,并在 MySQL 中显示其他记录
要排除记录,请使用 MySQL NOT IN()。我们首先创建一个表 -
mysql> create table DemoTable ( Id int ); Query OK, 0 rows affected (0.64 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(2); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(3); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(5); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(6); Query OK, 1 row affected (0.12 sec)
使用 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)
广告