查询 MySQL 查询以从表中排除某些值
使用 NOT IN() 从表中排除一些值。
我们首先创建一个表 -
mysql> create table DemoTable791 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100) ); Query OK, 0 rows affected (0.61 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable791(FirstName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable791(FirstName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable791(FirstName) values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable791(FirstName) values('Mike'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable791(FirstName) values('Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable791(FirstName) values('Carol'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable791(FirstName) values('Adam'); Query OK, 1 row affected (0.09 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable791;
这会产生以下输出 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Robert | | 3 | David | | 4 | Mike | | 5 | Bob | | 6 | Carol | | 7 | Adam | +----+-----------+ 7 rows in set (0.00 sec)
下面是使用 MySQL 中的 NOT IN() 从表中排除某些值的查询 -
mysql> select *from DemoTable791 where Id NOT IN(4,5,6);
这会产生以下输出 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Robert | | 3 | David | | 7 | Adam | +----+-----------+ 4 rows in set (0.00 sec)
广告