使用MySQL从重复的行中仅返回一行
要仅从重复行中返回单行,请使用DISTINCT关键字-
mysql> create table DemoTable1998 ( Name varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1998 values('Robert'); mysql> insert into DemoTable1998 values('Chris'); mysql> insert into DemoTable1998 values('Robert'); mysql> insert into DemoTable1998 values('David'); mysql> insert into DemoTable1998 values('Bob'); mysql> insert into DemoTable1998 values('David');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1998;
这将产生以下输出-
+--------+ | Name | +--------+ | Robert | | Chris | | Robert | | David | | Bob | | David | +--------+ 6 rows in set (0.00 sec)
这是从重复项仅返回一行的查询-
mysql> select distinct * from DemoTable1998;
这将产生以下输出-
+--------+ | Name | +--------+ | Robert | | Chris | | David | | Bob | +--------+ 4 rows in set (0.01 sec)