使用 MySQL 从不存在 value 的表中选择?
为此,您可以使用 NOT IN() −
mysql> create table DemoTable1991 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20) ); Query OK, 0 rows affected (0.61 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1991(StudentName) values('Chris'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1991(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1991(StudentName) values('David'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1991(StudentName) values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1991(StudentName) values('Mike'); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1991;
这将产生以下输出 −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Bob | | 3 | David | | 4 | Sam | | 5 | Mike | +-----------+-------------+ 5 rows in set (0.03 sec)
以下是从表中选择 * 的查询,其中不存在 value
mysql> select * from DemoTable1991 where StudentName NOT IN('Bob','Sam','Mike');
这将产生以下输出 −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 3 | David | +-----------+-------------+ 2 rows in set (0.04 sec)
广告