让我们首先创建一个表-
mysql> create table DemoTable ( Id int, FirstName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(100,'Chris'); mysql> insert into DemoTable values(100,'Robert'); mysql> insert into DemoTable values(100,'Mike'); mysql> insert into DemoTable values(100,'Sam'); mysql> insert into DemoTable values(101,'David'); mysql> insert into DemoTable values(101,'Robert'); mysql> insert into DemoTable values(210,'Chris'); mysql> insert into DemoTable values(210,'Bob'); mysql> insert into DemoTable values(210,'Sam');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Chris | | 100 | Robert | | 100 | Mike | | 100 | Sam | | 101 | David | | 101 | Robert | | 210 | Chris | | 210 | Bob | | 210 | Sam | +------+-----------+ 9 rows in set (0.00 sec)
这是从具有特定条件的重复项中查找单个值的查询-
mysql> select distinct Id from DemoTable where Id not in ( select Id from DemoTable where FirstName='Chris' );
这将产生以下输出-
+------+ | Id | +------+ | 101 | +------+ 1 row in set (0.08 sec)