从 MySQL 获取具有较高值的三条记录
mysqlmysqli database更新于 2024/8/13 16:12:17
首先我们创建一个表 −
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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

