只从重复的行中返回一行 MySQL
若想只从重复行中返回一行,请使用 DISTINCT 关键字 -
mysql> create table DemoTable1998 ( Name varchar(20) ); Query OK, 0 rows affected (0.61 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1998 values('Robert'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1998 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1998 values('Robert'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable1998 values('David'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1998 values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1998 values('David'); Query OK, 1 row affected (0.10 sec)
使用 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)
广告