目录
[一、先搭个 "舞台":创建表与插入数据](#一、先搭个 “舞台”:创建表与插入数据)
[1. 创建表](#1. 创建表)
[2. 插入数据:单行 / 多行 / 指定列](#2. 插入数据:单行 / 多行 / 指定列)
[二、数据查询:从 "全表查" 到 "精准筛"](#二、数据查询:从 “全表查” 到 “精准筛”)
[1. 基础查询:查列、查表达式、取别名](#1. 基础查询:查列、查表达式、取别名)
[(3)查询表达式(计算 / 常量)](#(3)查询表达式(计算 / 常量))
[2. 去重查询:distinct](#2. 去重查询:distinct)
[3. 条件筛选:where的常用操作符](#3. 条件筛选:where的常用操作符)
[三、结果排序:order by](#三、结果排序:order by)
[MySQL 基础查询语法速查表](#MySQL 基础查询语法速查表)
作为后端开发的 "基本功",MySQL 的 CRUD 操作是每个开发者都绕不开的知识点。今天我就以 "学生信息 + 考试成绩" 为案例,带你从表的创建 到复杂查询,手把手掌握 MySQL 的基础操作(代码全程小写)。
一、先搭个 "舞台":创建表与插入数据
我们先创建两个表:students(学生信息表)和exam_result(考试成绩表),再插入测试数据。
1. 创建表
sql
-- 学生信息表
create table students (
id int unsigned primary key auto_increment,
sn int not null unique comment '学号',
name varchar(20) not null,
qq varchar(20)
);
-- 考试成绩表
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 '英语成绩'
);
2. 插入数据:单行 / 多行 / 指定列
插入数据是create的核心,常见的插入方式有 3 种:
(1)单行全列插入
sql
insert into students values (100, 10000, '唐三藏', null);
insert into students values (101, 10001, '孙悟空', '11111');
(2)多行指定列插入
如果不需要插入所有列,可以指定列名(列和值的顺序要对应):
sql
insert into students (id, sn, name) values
(102, 20001, '孟姜德'),
(103, 20002, '孙仲谋');
(3)插入冲突处理
如果插入数据时遇到主键 / 唯一键冲突 ,可以用on duplicate key update自动更新:
sql
-- 主键id=100已存在,冲突时更新sn和name
insert into students (id, sn, name) values (100, 10010, '唐大师')
on duplicate key update sn = 10010, name = '唐大师';
也可以用replace(冲突时删除旧数据再插入新数据):
sql
-- 学号sn=20001已存在,冲突时替换
replace into students (sn, name) values (20001, '曹阿瞒');
二、数据查询:从 "全表查" 到 "精准筛"
查询是 MySQL 中最常用的操作,我们从简单到复杂逐步拆解。
1. 基础查询:查列、查表达式、取别名
(1)全表查询(谨慎用*)
sql
-- 不推荐:数据量大时性能差
select * from exam_result;
(2)指定列查询
sql
select id, name, english from exam_result;
(3)查询表达式(计算 / 常量)
sql
-- 查"英语+10分"的结果
select id, name, english + 10 from exam_result;
-- 查总分(语文+数学+英语)
select id, name, chinese + math + english from exam_result;
(4)给结果取别名
用as(可省略)给列 / 表达式取别名,让结果更易读:
sql
select id, name, chinese + math + english as 总分 from exam_result;
2. 去重查询:distinct
如果结果中有重复值,可以用distinct去重:
sql
-- 去重后查数学成绩
select distinct math from exam_result;
3. 条件筛选:where的常用操作符
where是查询的 "过滤器",常用操作符分为比较运算 和逻辑运算两类。
(1)比较运算案例
| 操作符 | 说明 | 示例代码 |
|---|---|---|
>/< |
大于 / 小于 | select name, english from exam_result where english < 60;(英语不及格) |
between...and |
范围匹配 | select name, chinese from exam_result where chinese between 80 and 90;(语文 80-90) |
in |
匹配列表中的任意值 | select name, math from exam_result where math in (58, 59, 98, 99);(数学特定分数) |
like |
模糊匹配(%任意字符,_单个字符) |
select name from exam_result where name like '孙%';(姓孙的同学) |
is null |
判断 null 值 | select name, qq from students where qq is not null;(有 QQ 的同学) |
(2)逻辑运算案例
用and/or/not组合多个条件:
sql
-- 语文>80 且 不姓孙
select name, chinese from exam_result
where chinese > 80 and name not like '孙%';
-- 姓孙 或 总分>200且语文<数学且英语>80
select name, chinese, math, english, chinese+math+english 总分
from exam_result
where name like '孙%' or (chinese+math+english > 200 and chinese < math and english > 80);
三、结果排序:order by
查询结果默认是 "插入顺序",用order by可以按指定列排序(asc升序,desc降序,默认 asc)。
sql
-- 按数学降序、英语升序、语文升序排序
select name, math, english, chinese from exam_result
order by math desc, english, chinese;
-- 按总分降序(别名也可以用在order by中)
select name, chinese+math+english 总分 from exam_result
order by 总分 desc;
四、分页查询:limit
当数据量很大时,用limit分页查询(避免一次性查全表):
sql
-- 语法:limit 条数 offset 起始下标(下标从0开始)
-- 第1页:查前3条
select id, name, math, english, chinese from exam_result
order by id limit 3 offset 0;
-- 第2页:从下标3开始,查3条
select id, name, math, english, chinese from exam_result
order by id limit 3 offset 3;
总结
MySQL 基础查询的核心就是 "选列(select)→ 选表(from)→ 过滤(where)→ 排序(order by)→ 分页(limit)",掌握这些操作就能应对大部分日常开发场景。
MySQL 基础查询语法速查表
| 语法分类 | 核心关键字 / 语法结构 | 功能说明 | 示例代码(完整可执行) | 注意事项 |
|---|---|---|---|---|
| 表操作 - 创建 | create table 表名 (列定义...) |
创建数据库表,定义列名、类型、约束等 | ```sql | |
| create table students ( | ||||
| id int unsigned primary key auto_increment, | ||||
| sn int not null unique comment ' 学号 ', | ||||
| name varchar(20) not null, | ||||
| qq varchar(20) | ||||
| ); | ||||
| ``` | 1. unsigned 表示无符号(非负);2. auto_increment 自动递增(仅主键可用);3. comment 为注释 |
|||
| 数据操作 - 插入 | insert into 表名 values (...) |
单行全列插入数据 | insert into students values (100, 10000, '唐三藏', null); |
列值顺序必须与表结构一致,主键自增时可填null或对应值 |
insert into 表名 (列1,列2...) values (...),(...); |
多行指定列插入数据 | insert into students (id, sn, name) values (102, 20001, '孟姜德'), (103, 20002, '孙仲谋'); |
列名与值需一一对应,未指定列取默认值或null(需允许为空) |
|
insert into ... on duplicate key update 列=值... |
主键 / 唯一键冲突时,更新原有数据 | insert into students (id, sn, name) values (100, 10010, '唐大师') on duplicate key update sn=10010, name='唐大师'; |
仅针对主键或唯一键冲突生效 | |
replace into 表名 values (...) |
主键 / 唯一键冲突时,删除旧数据再插入新数据 | replace into students (sn, name) values (20001, '曹阿瞒'); |
会删除原有数据,谨慎使用(可能丢失关联数据) | |
| 数据操作 - 查询 | select * from 表名; |
全表查询所有列数据 | select * from exam_result; |
数据量大时性能差,生产环境不推荐使用 |
select 列1,列2... from 表名; |
指定列查询数据 | select id, name, english from exam_result; |
列名可按需求排序,无需与表结构一致 | |
select 表达式 from 表名; |
查询计算结果、常量等 | select id, name, chinese + math + english from exam_result; |
表达式支持算术运算(+/-/*/÷)、字符串拼接等 | |
select 列/表达式 as 别名 from 表名; |
给查询结果列 / 表达式取别名,简化显示 | select id, name, chinese + math + english as 总分 from exam_result; |
as 可省略,别名含空格时需用反引号`````包裹 |
|
select distinct 列 from 表名; |
去重查询,去除结果集中的重复记录 | select distinct math from exam_result; |
distinct 作用于所有指定列(多列时需全部相同才会去重) |
|
| 查询条件筛选 | select ... from 表名 where 条件; |
按指定条件过滤数据,仅返回符合条件的记录 | 基础比较:select name, english from exam_result where english < 60; |
1. where 后不支持别名;2. 判空用is null/is not null,不可用= |
比较操作符:>,<,=,>=,<=,!= |
基础数值 / 字符串比较 | 范围匹配:select name, chinese from exam_result where chinese between 80 and 90; |
between...and 是闭区间(包含边界值) |
|
集合匹配:in (值1,值2...) |
判断列值是否在指定集合中 | select name, math from exam_result where math in (58, 59, 98, 99); |
适合少量值匹配,大量值推荐用join |
|
模糊匹配:like '匹配规则' |
字符串模糊查询,%匹配任意字符,_匹配单个字符 |
select name from exam_result where name like '孙%'; |
1. % 可放在任意位置;2. 区分大小写(取决于数据库编码) |
|
逻辑操作符:and,or,not |
组合多个查询条件,实现复杂筛选 | select name, chinese from exam_result where chinese > 80 and name not like '孙%'; |
优先级:not > and > or,可通过括号()改变优先级 |
|
| 结果排序 | select ... order by 列1 asc/desc, 列2...; |
按指定列对查询结果排序,asc升序(默认),desc降序 |
select name, math, english from exam_result order by math desc, english asc; |
1. 可按别名排序;2. 多列排序时,先按第一列排,再按第二列排 |
| 分页查询 | select ... limit 条数 offset 起始下标; |
分页获取数据,避免一次性返回大量结果 | 第 2 页(每页 3 条):select id, name from exam_result order by id limit 3 offset 3; |
1. 起始下标从 0 开始;2. limit 条数 offset 下标 可简写为 limit 下标, 条数 |
注意
语法顺序固定:select → from → where → order by → limit(不可打乱顺序);