MySQL查询找到得分最高的两个
为此,请使用聚合函数MAX()
。让我们首先创建一个表-
mysql> create table DemoTable1383 -> ( -> Id int, -> PlayerScore int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1383 values(200,78); mysql> insert into DemoTable1383 values(200,89); mysql> insert into DemoTable1383 values(200,89); mysql> insert into DemoTable1383 values(200,87); mysql> insert into DemoTable1383 values(203,97); mysql> insert into DemoTable1383 values(200,57);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1383;
这将产生以下输出-
+------+-------------+ | Id | PlayerScore | +------+-------------+ | 200 | 78 | | 200 | 89 | | 200 | 89 | | 200 | 87 | | 203 | 97 | | 200 | 57 | +------+-------------+ 6 rows in set (0.00 sec)
以下是查找最高分数并显示不同值的查询-
mysql> select Id,max(PlayerScore) from DemoTable1383 group by Id order by max(PlayerScore) DESC;
这将产生以下输出-
+------+------------------+ | Id | max(PlayerScore) | +------+------------------+ | 203 | 97 | | 200 | 89 | +------+------------------+ 2 rows in set (0.00 sec)