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)
广告