查詢最高兩分的 MySQL 查詢


為此,請使用聚合函式 MAX()。我們首先建立一個表 -

mysql> create table DemoTable1383
   -> (
   -> Id int,
   -> PlayerScore int
   -> );
Query OK, 0 rows affected (0.90 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable1383 values(200,78);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1383 values(200,89);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1383 values(200,89);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1383 values(200,87);
Query OK, 1 row affected (0.29 sec)
mysql> insert into DemoTable1383 values(203,97);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1383 values(200,57);
Query OK, 1 row affected (0.17 sec)

使用 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)

更新於: 11-Nov-2019

478 次瀏覽

Kickstart Your Career

完成課程,獲得證書

開始學習
廣告
© . All rights reserved.