为此,可以使用DISTINCT关键字。让我们首先创建一个表-
mysql> create table DemoTable1801 ( Name varchar(20), Score int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1801 values('John',98); mysql> insert into DemoTable1801 values('John',98); mysql> insert into DemoTable1801 values('John',99); mysql> insert into DemoTable1801 values('Carol',99);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1801;
这将产生以下输出-
+-------+-------+ | Name | Score | +-------+-------+ | John | 98 | | John | 98 | | John | 99 | | Carol | 99 | +-------+-------+ 4 rows in set (0.00 sec)
这是要为单独的ID分组的查询-
mysql> select distinct Name,Score from DemoTable1801;
这将产生以下输出-
+-------+-------+ | Name | Score | +-------+-------+ | John | 98 | | John | 99 | | Carol | 99 | +-------+-------+ 3 rows in set (0.00 sec)