MySQL查詢按降序選擇除第一行之外的行?


首先,我們建立一個表 -

mysql> create table DemoTable
   -> (
   -> Amount int
   -> );
Query OK, 0 rows affected (0.50 sec)

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

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(20);
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable values(30);
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable values(40);
Query OK, 1 row affected (0.15 sec)

使用select語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

輸出

+--------+
| Amount |
+--------+
|     10 |
|     20 |
|     30 |
|     40 |
+--------+
4 rows in set (0.00 sec)

以下是按降序選擇除第一行之外的行:-

mysql> select *from DemoTable WHERE Amount NOT IN (SELECT MAX(Amount) from DemoTable) ORDER BY Amount DESC;

輸出

+--------+
| Amount |
+--------+
|     30 |
|     20 |
|     10 |
+--------+
3 rows in set (0.05 sec)

更新於:2019年7月30日

773次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告