【MySQL】 基本查询(上)

发布于:2025-02-15 ⋅ 阅读:(15) ⋅ 点赞:(0)

欢迎拜访雾里看山-CSDN博客
本篇主题:【MySQL】 基本查询(上)
发布时间:2025.2.14
隶属专栏MySQL

在这里插入图片描述
CRUD : Create(创建), Retrieve(读取),Update(更新),Delete(删除)

Create

基本知识

语法:

INSERT [INTO] table_name
	[(column [, column] ...)]
	VALUES (value_list) [, (value_list)] ...

案例:

mysql> create table students(
    -> id int unsigned primary key auto_increment,
    -> sn int not null unique key,
    -> name varchar(20) not null,
    -> qq varchar(32) unique key
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| sn    | int(11)          | NO   | UNI | NULL    |                |
| name  | varchar(20)      | NO   |     | NULL    |                |
| qq    | varchar(32)      | YES  | UNI | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

直接插入

单行数据,指定列插入

into 可以省略,但是建议带上。

mysql> insert into students (sn, name, qq) values (123, '张飞', '4567890');
Query OK, 1 row affected (0.00 sec)

mysql> select * from students;
+----+-----+--------+---------+
| id | sn  | name   | qq      |
+----+-----+--------+---------+
|  1 | 123 | 张飞   | 4567890 |
+----+-----+--------+---------+
1 row in set (0.00 sec)

单行数据,全列插入

into 可以省略,但是建议带上。

mysql> insert into students values (2, 124, '刘备', '4567891');
Query OK, 1 row affected (0.00 sec)

mysql> select * from students;
+----+-----+--------+---------+
| id | sn  | name   | qq      |
+----+-----+--------+---------+
|  1 | 123 | 张飞   | 4567890 |
|  2 | 124 | 刘备   | 4567891 |
+----+-----+--------+---------+
2 rows in set (0.00 sec)

多行数据,指定列插入

每组数据之间用,(英文符号)隔开

mysql> insert into students (sn, name, qq) values (125, '关羽', '4562890'),(126, '诸葛亮', '423613719'),(127, '赵云', '3728140');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from students;
+----+-----+-----------+-----------+
| id | sn  | name      | qq        |
+----+-----+-----------+-----------+
|  1 | 123 | 张飞      | 4567890   |
|  2 | 124 | 刘备      | 4567891   |
|  3 | 125 | 关羽      | 4562890   |
|  4 | 126 | 诸葛亮    | 423613719 |
|  5 | 127 | 赵云      | 3728140   |
+----+-----+-----------+-----------+
5 rows in set (0.00 sec)

多行数据,全列插入

每组数据之间用,(英文符号)隔开

mysql> insert into students values (10, 128, '曹操', '45637891'),(11, 129, '许攸','14723193'),(12, 130, '许褚','4723012');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from students;
+----+-----+-----------+-----------+
| id | sn  | name      | qq        |
+----+-----+-----------+-----------+
|  1 | 123 | 张飞      | 4567890   |
|  2 | 124 | 刘备      | 4567891   |
|  3 | 125 | 关羽      | 4562890   |
|  4 | 126 | 诸葛亮    | 423613719 |
|  5 | 127 | 赵云      | 3728140   |
| 10 | 128 | 曹操      | 45637891  |
| 11 | 129 | 许攸      | 14723193  |
| 12 | 130 | 许褚      | 4723012   |
+----+-----+-----------+-----------+
8 rows in set (0.00 sec)

插入替换

主键或者唯一键冲突

-- 主键冲突
mysql> insert into students values (12, 133, '荀彧', '45167891');
ERROR 1062 (23000): Duplicate entry '12' for key 'PRIMARY'

-- 唯一键冲突
mysql> insert into students values (14, 132, '荀彧', '45167891');
ERROR 1062 (23000): Duplicate entry '132' for key 'sn'

插入时更新

语法:

INSERT ... ON DUPLICATE KEY UPDATE

案例:

mysql> insert into students values (11, 131, 'xuyou', '112121') on duplicate key update sn=131, name='xuyou', qq='112121';
Query OK, 2 rows affected (0.00 sec)

mysql> insert into students values (13, 132, '貂蝉', '1121211') on duplicate key update sn=132, name='貂蝉', qq='1121211';
Query OK, 1 row affected (0.01 sec)

mysql> insert into students values (13, 132, '貂蝉', '1121211') on duplicate key update sn=132, name='貂蝉', qq='1121211';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from students;
+----+-----+-----------+-----------+
| id | sn  | name      | qq        |
+----+-----+-----------+-----------+
|  1 | 123 | 张飞      | 4567890   |
|  2 | 124 | 刘备      | 4567891   |
|  3 | 125 | 关羽      | 4562890   |
|  4 | 126 | 诸葛亮    | 423613719 |
|  5 | 127 | 赵云      | 3728140   |
| 10 | 128 | 曹操      | 45637891  |
| 11 | 131 | xuyou     | 112121    |
| 12 | 130 | 许褚      | 4723012   |
| 13 | 132 | 貂蝉      | 1121211   |
+----+-----+-----------+-----------+
9 rows in set (0.00 sec)

受影响的行数不同,表示插入的情况也不同。

  • 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
  • 1 row affected: 表中没有冲突数据,数据被插入
  • 2 row affected: 表中有冲突数据,并且数据已经被更新
通过 MySQL 函数获取受到影响的数据行数
mysql> select row_count();
+-------------+
| row_count() |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

插入时替换

  • 主键 或者 唯一键 没有冲突,则直接插入
  • 主键 或者 唯一键 如果冲突,则删除后再插入
mysql> replace into students (sn, name, qq) values (133, '许攸', '12345612');
Query OK, 1 row affected (0.01 sec)

mysql> replace into students (sn, name, qq) values (133, '许攸1', '12345612');
Query OK, 2 rows affected (0.02 sec)

mysql> replace into students (sn, name, qq) values (132, '许攸1', '12345612');
Query OK, 3 rows affected (0.01 sec)

mysql> select * from students;
+----+-----+-----------+-----------+
| id | sn  | name      | qq        |
+----+-----+-----------+-----------+
|  1 | 123 | 张飞      | 4567890   |
|  2 | 124 | 刘备      | 4567891   |
|  3 | 125 | 关羽      | 4562890   |
|  4 | 126 | 诸葛亮    | 423613719 |
|  5 | 127 | 赵云      | 3728140   |
| 10 | 128 | 曹操      | 45637891  |
| 11 | 131 | xuyou     | 112121    |
| 12 | 130 | 许褚      | 4723012   |
| 16 | 132 | 许攸1     | 12345612  |
+----+-----+-----------+-----------+
9 rows in set (0.00 sec)

受影响的行数不同,表示插入的情况也不同。

  • 1 row affected: 表中没有冲突数据,数据被插入
  • 2 row affected: 表中有一行冲突数据,删除后重新插入
  • n row affected: 表中有 n-1 行冲突数据,删除后重新插入

Retrieve

基本知识

语法:

SELECT
	[DISTINCT] {* | {column [, column] ...}
	[FROM table_name]
	[WHERE ...]
	[ORDER BY column [ASC | DESC], ...]
	LIMIT ...

案例:

-- 创建表结构
mysql> CREATE TABLE exam_result (
    -> id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    -> name VARCHAR(20) NOT NULL COMMENT '同学姓名',
    -> chinese float DEFAULT 0.0 COMMENT '语文成绩',
    -> math float DEFAULT 0.0 COMMENT '数学成绩',
    -> english float DEFAULT 0.0 COMMENT '英语成绩'
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc exam_result;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20)      | NO   |     | NULL    |                |
| chinese | float            | YES  |     | 0       |                |
| math    | float            | YES  |     | 0       |                |
| english | float            | YES  |     | 0       |                |
+---------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

-- 插入测试数据
mysql> INSERT INTO exam_result (name, chinese, math, english) VALUES
    -> ('唐三藏', 67, 98, 56),
    -> ('孙悟空', 87, 78, 77),
    -> ('猪悟能', 88, 98, 90),
    -> ('曹孟德', 82, 84, 67),
    -> ('刘玄德', 55, 85, 45),
    -> ('孙权', 70, 73, 78),
    -> ('宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

select 列

全列查询

通常情况下不建议使用 * 进行全列查询

  1. 查询的列越多,意味着需要传输的数据量越大;
  2. 可能会影响到索引的使用。(索引待后面文章讲解)
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

指定列进行查询

指定列的顺序不需要按定义表的顺序来

mysql> select id, math, name from exam_result;
+----+------+-----------+
| id | math | name      |
+----+------+-----------+
|  1 |   98 | 唐三藏    |
|  2 |   78 | 孙悟空    |
|  3 |   98 | 猪悟能    |
|  4 |   84 | 曹孟德    |
|  5 |   85 | 刘玄德    |
|  6 |   73 | 孙权      |
|  7 |   65 | 宋公明    |
+----+------+-----------+
7 rows in set (0.00 sec)

查询字段为表达式

  1. 表达式不包含字段
mysql> select id, name, 10 from exam_result;
+----+-----------+----+
| id | name      | 10 |
+----+-----------+----+
|  1 | 唐三藏    | 10 |
|  2 | 孙悟空    | 10 |
|  3 | 猪悟能    | 10 |
|  4 | 曹孟德    | 10 |
|  5 | 刘玄德    | 10 |
|  6 | 孙权      | 10 |
|  7 | 宋公明    | 10 |
+----+-----------+----+
7 rows in set (0.00 sec)
  1. 表达式包含一个字段
mysql> select id, name, 10+math from exam_result;
+----+-----------+---------+
| id | name      | 10+math |
+----+-----------+---------+
|  1 | 唐三藏    |     108 |
|  2 | 孙悟空    |      88 |
|  3 | 猪悟能    |     108 |
|  4 | 曹孟德    |      94 |
|  5 | 刘玄德    |      95 |
|  6 | 孙权      |      83 |
|  7 | 宋公明    |      75 |
+----+-----------+---------+
7 rows in set (0.03 sec)
  1. 表达式包含多个字段
mysql> select id, name, chinese+math+english from exam_result;
+----+-----------+----------------------+
| id | name      | chinese+math+english |
+----+-----------+----------------------+
|  1 | 唐三藏    |                  221 |
|  2 | 孙悟空    |                  242 |
|  3 | 猪悟能    |                  276 |
|  4 | 曹孟德    |                  233 |
|  5 | 刘玄德    |                  185 |
|  6 | 孙权      |                  221 |
|  7 | 宋公明    |                  170 |
+----+-----------+----------------------+
7 rows in set (0.00 sec)

为查询结果指定别名

语法:

SELECT column [AS] alias_name [...] FROM table_name;

as可以省略

案例:

mysql> select id, name, chinese+math+english as total from exam_result;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  1 | 唐三藏    |   221 |
|  2 | 孙悟空    |   242 |
|  3 | 猪悟能    |   276 |
|  4 | 曹孟德    |   233 |
|  5 | 刘玄德    |   185 |
|  6 | 孙权      |   221 |
|  7 | 宋公明    |   170 |
+----+-----------+-------+
7 rows in set (0.00 sec)

结果去重

去重前

mysql> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   98 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
7 rows in set (0.00 sec)

去重后

mysql> select distinct math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
6 rows in set (0.00 sec)

where条件

比较运算符

运算符 说明
> , >=, <, <= 大于,大于等于,小于,小于等于
= 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL
<=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1)
!=, <> 不等于
BETWEEN a0 AND a1 范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1)
IN (option, …) 如果是 option 中的任意一个,返回 TRUE(1)
IS NULL 是 NULL
IS NOT NULL 不是 NULL
LIKE 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符

逻辑运算符

运算符 说明
AND 多个条件必须都为 TRUE(1),结果才是 TRUE(1)
OR 任意一个条件为 TRUE(1), 结果为 TRUE(1)
NOT 条件为 TRUE(1),结过为 FALSE(0)

案例

  1. 英语不及格的同学及英语成绩(<60)
mysql> select name, english from exam_result where english<60;
+-----------+---------+
| name      | english |
+-----------+---------+
| 唐三藏    |      56 |
| 刘玄德    |      45 |
| 宋公明    |      30 |
+-----------+---------+
3 rows in set (0.00 sec)
  1. 语文成绩在 [80, 90] 分的同学及语文成绩

使用 AND 进行条件连接

mysql> select name, chinese from exam_result where chinese>=80 and chinese<=90;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 孙悟空    |      87 |
| 猪悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+
3 rows in set (0.00 sec)

使用 BETWEEN ... AND ... 条件

mysql> select name, chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 孙悟空    |      87 |
| 猪悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+
3 rows in set (0.00 sec)
  1. 数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩的同学及数学成绩

使用 OR 进行条件连接

mysql> select name, math from exam_result where math=58 or math=59 or math=98 or math=99;
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+
2 rows in set (0.01 sec)

使用 IN 条件

mysql> select name, math from exam_result where math in (58, 59, 98, 99);
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+
2 rows in set (0.01 sec)

  1. 姓孙的同学 及 孙某同学

% 匹配任意多个(包括 0 个)任意字符

mysql> select name from exam_result where name like '孙%';
+-----------+
| name      |
+-----------+
| 孙悟空    |
| 孙权      |
+-----------+
2 rows in set (0.00 sec)

_ 匹配严格的一个任意字符

mysql> select name from exam_result where name like '孙_';
+--------+
| name   |
+--------+
| 孙权   |
+--------+
1 row in set (0.00 sec)

  1. 语文成绩好于英语成绩的同学

WHERE 条件中比较运算符两侧都是字段

mysql> select name, chinese, english from exam_result where chinese > english;
+-----------+---------+---------+
| name      | chinese | english |
+-----------+---------+---------+
| 唐三藏    |      67 |      56 |
| 孙悟空    |      87 |      77 |
| 曹孟德    |      82 |      67 |
| 刘玄德    |      55 |      45 |
| 宋公明    |      75 |      30 |
+-----------+---------+---------+
5 rows in set (0.00 sec)
  1. 总分在 200 分以下的同学

WHERE 条件中使用表达式
别名不能用在 WHERE 条件中

mysql> select name, chinese+math+english as 总分 from exam_result where chinese+math+english<200;
+-----------+--------+
| name      | 总分   |
+-----------+--------+
| 刘玄德    |    185 |
| 宋公明    |    170 |
+-----------+--------+
2 rows in set (0.00 sec)
  1. 语文成绩 > 80 并且不姓孙的同学

ANDNOT 的使用

mysql> select name, chinese from exam_result where chinese>80 and name not like '孙%';
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 猪悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+
2 rows in set (0.00 sec)
  1. 孙某同学,否则要求总成绩 > 200 并且 语文成绩 < 数学成绩 并且 英语成绩 > 80

综合查询

mysql> select name, chinese, math, english, chinese+math+english as 总分 
	->from exam_result 
	->where name like '孙_' or (chinese+math+english>200 and chinese<math and english>80);
+-----------+---------+------+---------+--------+
| name      | chinese | math | english | 总分   |
+-----------+---------+------+---------+--------+
| 猪悟能    |      88 |   98 |      90 |    276 |
| 孙权      |      70 |   73 |      78 |    221 |
+-----------+---------+------+---------+--------+
2 rows in set (0.00 sec)
  1. NULL 的查询
mysql> create table test(
    -> id int, 
    -> name varchar(20)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test values (1,'张三');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (null,'张三');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (1,null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (null,null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (1,'');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 张三   |
| NULL | 张三   |
|    1 | NULL   |
| NULL | NULL   |
|    1 |        |
+------+--------+
5 rows in set (0.00 sec)

mysql> select * from test where name is null;
+------+------+
| id   | name |
+------+------+
|    1 | NULL |
| NULL | NULL |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from test where name is  not null;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 张三   |
| NULL | 张三   |
|    1 |        |
+------+--------+
3 rows in set (0.00 sec)

NULL NULL的比较,=<=>的区别

SELECT NULL = NULL, NULL = 1, NULL = 0;
+-------------+----------+----------+
| NULL = NULL | NULL = 1 | NULL = 0 |
+-------------+----------+----------+
|        NULL |     NULL |     NULL |
+-------------+----------+----------+
1 row in set (0.00 sec)

SELECT NULL <=> NULL, NULL <=> 1, NULL <=> 0;
+---------------+------------+------------+
| NULL <=> NULL | NULL <=> 1 | NULL <=> 0 |
+---------------+------------+------------+
|             1 |          0 |          0 |
+---------------+------------+------------+
1 row in set (0.00 sec)

结果排序

语法:

SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
  • ASC 为升序(从小到大)
  • DESC 为降序(从大到小)
  • 默认为 ASC
  • 没有 ORDER BY 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序

案例:

  1. 同学及数学成绩,按数学成绩升序显示
mysql> select name, math from exam_result order by math asc;
+-----------+------+
| name      | math |
+-----------+------+
| 宋公明    |   65 |
| 孙权      |   73 |
| 孙悟空    |   78 |
| 曹孟德    |   84 |
| 刘玄德    |   85 |
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+
7 rows in set (0.02 sec)
  1. id及姓名,按姓名排序显示

NULL 视为比任何值都小,升序出现在最上面

mysql> select id,name from test order by name asc;
+------+--------+
| id   | name   |
+------+--------+
|    1 | NULL   |
| NULL | NULL   |
|    1 |        |
|    1 | 张三   |
| NULL | 张三   |
+------+--------+
5 rows in set (0.00 sec)

NULL 视为比任何值都小,降序出现在最下面

mysql> select id,name from test order by name desc;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 张三   |
| NULL | 张三   |
|    1 |        |
|    1 | NULL   |
| NULL | NULL   |
+------+--------+
5 rows in set (0.00 sec)
  1. 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示

多字段排序,排序优先级随书写顺序

mysql> select name, math, english, chinese from exam_result order by math desc, english asc, chinese asc;
+-----------+------+---------+---------+
| name      | math | english | chinese |
+-----------+------+---------+---------+
| 唐三藏    |   98 |      56 |      67 |
| 猪悟能    |   98 |      90 |      88 |
| 刘玄德    |   85 |      45 |      55 |
| 曹孟德    |   84 |      67 |      82 |
| 孙悟空    |   78 |      77 |      87 |
| 孙权      |   73 |      78 |      70 |
| 宋公明    |   65 |      30 |      75 |
+-----------+------+---------+---------+
7 rows in set (0.00 sec)
  1. 查询同学及总分,由高到低

ORDER BY 中可以使用表达式

mysql> select name, chinese+english+math from exam_result order by chinese+english+math desc;
+-----------+----------------------+
| name      | chinese+english+math |
+-----------+----------------------+
| 猪悟能    |                  276 |
| 孙悟空    |                  242 |
| 曹孟德    |                  233 |
| 唐三藏    |                  221 |
| 孙权      |                  221 |
| 刘玄德    |                  185 |
| 宋公明    |                  170 |
+-----------+----------------------+
7 rows in set (0.00 sec)

ORDER BY 子句中可以使用列别名

mysql> select name, chinese+english+math as total from exam_result order by total desc;
+-----------+-------+
| name      | total |
+-----------+-------+
| 猪悟能    |   276 |
| 孙悟空    |   242 |
| 曹孟德    |   233 |
| 唐三藏    |   221 |
| 孙权      |   221 |
| 刘玄德    |   185 |
| 宋公明    |   170 |
+-----------+-------+
7 rows in set (0.00 sec)
  1. 查询姓孙的同学或者姓曹的同学数学成绩,结果按数学成绩由高到低显示

结合WHERE 子句 和 ORDER BY 子句

mysql> select name, math from exam_result
    -> where name like '孙%' or name like '曹%'
    -> order by math desc;
+-----------+------+
| name      | math |
+-----------+------+
| 曹孟德    |   84 |
| 孙悟空    |   78 |
| 孙权      |   73 |
+-----------+------+
3 rows in set (0.00 sec)

筛选分页结果

语法

起始下标为 0

  1. 从 s 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n
  1. 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
;
  1. 从 s 开始,筛选 n 条结果,比第二种用法更明确,建议使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;

只有数据准备好了,你才要显示,limit的本质功能是显示.

案例

建议:对未知表进行查询时,最好加一条 LIMIT 1,避免因为表中数据过大,查询全表数据导致数据库卡死按 id 进行分页,每页 3 条记录,分别显示1、2、3页

第一页

ysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 0;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)

第二页

mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 3;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)

第三页,如果结果不足 3 个,不会有影响

mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 6;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)

⚠️ 写在最后:以上内容是我在学习以后得一些总结和概括,如有错误或者需要补充的地方欢迎各位大佬评论或者私信我交流!!!