mysql 基本查询(crud)内容总结
该文档系统讲解了 mysql 中表的增删改查(crud)操作,即 create(创建)、retrieve(读取)、update(更新)、delete(删除),并附带大量案例与语法说明。以下按章节梳理核心内容。
一、create(插入数据)
基本语法:
sql
insert [into] table_name [(column [, column] ...)]
values (value_list) [, (value_list)] ...
1. 单行数据 + 全列插入
- value_list 数量必须与表定义列的数量及顺序一致。
- 若不指定 id,mysql 会使用默认值(自增)填充。
2. 多行数据 + 指定列插入
- 一次插入多行,value_list 数量必须与指定列数量及顺序一致。
- 示例:
insert into students (id, sn, name) values (102, 20001, '曹孟德'), (103, 20002, '孙仲谋');
3. 插入否则更新(on duplicate key update)
- 当主键或唯一键冲突时,可选择同步更新操作。
- 受影响行数含义:
- 0 行:冲突数据与 update 值相等,未更新。
- 1 行:无冲突,插入成功。
- 2 行:有冲突,数据被更新。
- 可用
select row_count();获取受影响行数。
4. 替换(replace)
- 无冲突则直接插入;有冲突则先删除再插入。
- 受影响行数:1 行表示插入;2 行表示删除后重新插入。
二、retrieve(查询数据)
基本语法:
sql
select [distinct] {* | {column [, column] ...}}
[from table_name]
[where ...]
[order by column [asc | desc], ...]
limit ...
1. select 列
- 全列查询 :
select * from ...(不建议,数据量大且可能影响索引)。 - 指定列查询:列顺序可与表定义顺序不同。
- 表达式查询:可包含常量、单字段、多字段表达式。
- 别名 :
select column [as] alias_name ... - 去重 :
select distinct column from ...
2. where 条件
- 比较运算符 :
>、>=、<、<=、=、<=>、!=、<>、between ... and ...、in (...)、is null、is not null、like - 逻辑运算符 :
and、or、not - 典型用法:
- 范围查询:
between 80 and 90 - 枚举查询:
in (58, 59, 98, 99) - 模糊匹配:
like '孙%'(% 匹配任意多个字符)、like '孙_'(_ 匹配一个字符) - 字段间比较:
where chinese > english - null 安全比较:
<=>与=的区别(null = null结果为 null,null <=> null结果为 true)
- 范围查询:
3. 结果排序(order by)
asc升序(默认),desc降序。- 支持多字段排序,优先级按书写顺序。
- 可使用表达式或列别名排序。
- null 视为比任何值都小。
4. 筛选分页(limit)
limit n:从 0 开始取 n 条。limit s, n:从 s 开始取 n 条。limit n offset s:更明确的写法,建议使用。- 建议对未知表查询时加
limit 1,避免全表扫描导致卡死。
三、update(更新数据)
语法:
sql
update table_name set column = expr [, column = expr ...]
[where ...] [order by ...] [limit ...]
- 可更新单列或多列。
- 更新值可基于原值计算,如
set math = math + 30(不支持math += 30)。 - 可结合
order by ... limit更新指定行。 - 慎用无 where 的全表更新。
四、delete(删除数据)
语法:
sql
delete from table_name [where ...] [order by ...] [limit ...]
- 可删除指定行或整表数据。
- 删除整表后,自增 id 不会重置(继续在原值上增长)。
截断表(truncate)
sql
truncate [table] table_name
- 只能整表操作,不能部分删除。
- 比 delete 快,但不经过真正的事务,无法回滚。
- 会重置
auto_increment项。
五、插入查询结果
语法:
sql
insert into table_name [(column [, column ...])] select ...
案例:删除表中重复记录
-
创建结构相同的空表:
create table no_duplicate_table like duplicate_table; -
插入去重数据:
insert into no_duplicate_table select distinct * from duplicate_table; -
重命名表实现原子去重:
sqlrename table duplicate_table to old_duplicate_table, no_duplicate_table to duplicate_table;
六、聚合函数
| 函数 | 说明 |
|---|---|
| count(distinct expr) | 返回数据数量 |
| sum(distinct expr) | 返回数据总和(非数字无意义) |
| avg(distinct expr) | 返回平均值(非数字无意义) |
| max(distinct expr) | 返回最大值(非数字无意义) |
| min(distinct expr) | 返回最小值(非数字无意义) |
count(*)不受 null 影响;count(qq)不统计 null。count(distinct math)统计去重后的数量。- 无结果时
sum返回 null。
七、group by 子句
语法:
sql
select column1, column2, ... from table group by column;
-
对指定列进行分组查询。
-
示例:
- 显示每个部门的平均工资和最高工资。
select avg(sal), min(sal), job, deptno from emp group by deptno, job;
-
having 与 group by 配合,对分组结果进行过滤:
sqlselect avg(sal) as myavg from emp group by deptno having myavg < 2000;- having 作用类似 where,但作用于分组后的结果。
该文档全面覆盖了 mysql 表的基本 crud 操作,从插入(单行/多行/冲突处理/替换)、查询(列选择、条件过滤、排序、分页)、更新、删除(含 truncate),到聚合函数、分组查询及实战练习,是一份体系完整的 mysql 入门与进阶学习资料。重点强调了:
- 查询时避免
select *,注意 limit 保护。 - 更新和删除操作慎用无 where 的全表操作。
truncate与delete的区别。group by与having的配合使用。- sql 关键字执行顺序对查询逻辑的影响。