MySQL查询从表中排除一些值
使用NOTIN()
从表中排除某些值。
让我们首先创建一个表-
mysql> create table DemoTable791 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable791(FirstName) values('Chris'); mysql> insert into DemoTable791(FirstName) values('Robert'); mysql> insert into DemoTable791(FirstName) values('David'); mysql> insert into DemoTable791(FirstName) values('Mike'); mysql> insert into DemoTable791(FirstName) values('Bob'); mysql> insert into DemoTable791(FirstName) values('Carol'); mysql> insert into DemoTable791(FirstName) values('Adam');
使用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)
以下是使用IN()
MySQL中的NOT从表中排除某些值的查询-
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)