从 MySQL 中获取三个具有较高值的数据
我们先创建一个表 −
mysql> create table DemoTable1614 -> ( -> StudentName varchar(20), -> StudentScore int -> ); Query OK, 0 rows affected (0.78 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1614 values('Adam',65); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1614 values('Chris',89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1614 values('Bob',58); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1614 values('Sam',98); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1614 values('Mike',87); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1614 values('Carol',79); Query OK, 1 row affected (0.12 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1614;
这将产生以下输出
+-------------+--------------+ | StudentName | StudentScore | +-------------+--------------+ | Adam | 65 | | Chris | 89 | | Bob | 58 | | Sam | 98 | | Mike | 87 | | Carol | 79 | +-------------+--------------+ 6 rows in set (0.00 sec)
以下是如何获取 3 个具有更高值的记录的查询 −
mysql> select * from DemoTable1614 order by StudentScore DESC LIMIT 3;
这将产生以下输出
+-------------+--------------+ | StudentName | StudentScore | +-------------+--------------+ | Sam | 98 | | Chris | 89 | | Mike | 87 | +-------------+--------------+ 3 rows in set (0.00 sec)
广告