使用ORDER BY对重复记录的数据进行排序。
让我们首先创建一个表-mysql> create table DemoTable788 (
FirstName varchar(100),
Score int
);
使用插入命令在表中插入一些记录-mysql> insert into DemoTable788 values('Chris',78);
mysql> insert into DemoTable788 values('Robert',67);
mysql> insert into DemoTable788 values('Chris',98);
mysql> insert into DemoTable788 values('Chris',56);
mysql> insert into DemoTable788 values('Robert',43);
mysql> insert into DemoTable788 values('Robert',97);
mysql> insert into DemoTable788 values('Chris',79);
使用select语句显示表中的所有记录-mysql> select *from DemoTable788;
这将产生以下输出-+-----------+-------+
| FirstName | Score |
+-----------+-------+
| Chris | 78 |
| Robert | 67 |
| Chris | 98 |
| Chris | 56 |
| Robert | 43 |
| Robert | 97 |
| Chris | 79 |
+-----------+-------+
7 rows in set (0.00 sec)
以下是对重复记录的数据进行排序的查询。我们正在为重复名称'Chris'排序数据-mysql> select Score from DemoTable788 where FirstName='Chris' order by Score DESC;
这将产生以下输出-+-------+
| Score |
+-------+
| 98 |
| 79 |
| 78 |
| 56 |
+-------+
4 rows in set (0.00 sec)