从表中获取前两个最高列值的 MySQL 查询?
要获取前两列最高列,请使用 ORDER BY。使用 LIMIT 2 仅获取前 2 −
select *from yourTableName order by yourColumnName DESC LIMIT 2;
我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.54 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(70); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(105); Query OK, 1 row affected (0.19 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
输出
+-------+ | Value | +-------+ | 90 | | 70 | | 40 | | 120 | | 98 | | 105 | +-------+ 6 rows in set (0.00 sec)
以下是获取表中前两个最高列值的查询 −
mysql> select *from DemoTable order by Value DESC LIMIT 2;
输出
+-------+ | Value | +-------+ | 120 | | 105 | +-------+ 2 rows in set (0.00 sec)
广告