极简数据库SQL,适配99%日常工作
工作高频常用SQL语句,适配日常查数、开发、改表、数据分析、运维场景。
01 万能查询模板
sql
select 字段/聚合函数
from 主表 t1
left join 关联表 t2 on t1.id = t2.t
where 行数据过滤(分组前、无聚合)
group by 分组字段
having 分组后过滤(只能用聚合)
order by 排序字段 asc/desc
limit 分页条数;
执行顺序:
text
FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT
关键区别:
where:分组前过滤原始数据,不支持聚合函数having:分组统计后过滤结果,必须搭配group by、只用聚合函数
02 建表 + 改表操作
1. 通用建表模板
sql
create table 表名(
id int primary key auto_increment,
name varchar(50) not null comment '名称',
status tinyint default 1 comment '状态',
create_time datetime default current_timestamp,
update_time datetime default current_timestamp
) comment '表备注信息';
2. 四大常用字段类型
| 类型 | 说明 |
|---|---|
| int | 整数(ID、状态、数量) |
| varchar(n) | 可变字符串(名称、地址,最常用) |
| char(n) | 定长字符串(手机号、身份证) |
| datetime | 年月日时分秒(时间字段首选) |
3. 四大核心约束
| 约束 | 说明 |
|---|---|
| primary key | 主键(唯一、非空,一表一个) |
| auto_increment | 自增(搭配主键 ID 使用) |
| not null | 非空(字段禁止为空) |
| unique | 唯一(值不重复,可空) |
4. 常用改表语句(高频运维)
sql
-- 新增字段
alter table 表名 add 字段名 类型(长度);
-- 修改字段类型
alter table 表名 modify 字段名 新类型;
-- 删除字段
alter table 表名 drop column 字段名;
-- 查看表结构
desc 表名;
-- 删除整张表
drop table 表名;
03 日常高频单表查询
1. 基础查询
sql
-- 查询所有数据
select * from 表名;
-- 查询指定字段(工作优先用,禁止随意用 *)
select 字段1, 字段2 from 表名;
-- 去重查询
select distinct 字段 from 表名;
-- 分页查询(最常用)
select * from 表名 limit 10; -- 前10条
select * from 表名 limit 0, 10; -- 第1页(从0开始,取10条)
2. 条件查询(工作核心)
sql
-- 精准匹配
select * from 表名 where 字段 = '值';
-- 模糊查询(包含XX)
select * from 表名 where 字段 like '%XX%';
-- 范围查询
select * from 表名 where 字段 between 值1 and 值2;
-- 固定多值查询
select * from 表名 where 字段 in (1, 2, 3);
-- 空值/非空查询(重点:不能用 =null)
select * from 表名 where 字段 is null;
select * from 表名 where 字段 is not null;
-- 多条件组合 and(且)/ or(或)/ not
select * from 表名 where 条件1 and 条件2;
select * from 表名 where 条件1 or 条件2;
3. 排序查询
sql
-- 升序(默认asc可省略)
select * from 表名 order by 字段 asc;
-- 降序(常用)
select * from 表名 order by 字段 desc;
-- 多字段排序
select * from 表名 order by 字段1 desc, 字段2 asc;
04 聚合统计 + 分组查询(数据必备)
1. 五大核心聚合函数
| 函数 | 说明 |
|---|---|
| count() | 统计行数 |
| sum() | 求和 |
| avg() | 平均值 |
| max() | 最大值 |
| min() | 最小值 |
重点区别:count(*) 统计所有行(包含 null);count(字段) 忽略 null 值。
2. 常用统计语句
sql
-- 常规全表统计
select count(*), sum(金额), avg(年龄), max(年龄), min(年龄) from 表名;
-- 分组统计(核心工作场景)
-- 按字段分组,筛选分组后数据
select 分组字段, count(*) as 数量
from 表名
where 前置条件
group by 分组字段
having count(*) > 2; -- 过滤分组结果
区别:
COUNT(*):统计所有行,包括 NULL。COUNT(字段):只统计该字段非 NULL 的行。
05 多表联查
场景:工作几乎只用左连接,其次等值连接,右连接极少用。
sql
-- 1. 内连接(只查两张表匹配成功的数据)
select a.字段, b.字段
from 表A a
inner join 表B b
on a.关联字段 = b.关联字段;
-- 2. 左连接(工作首选!保留左表全部数据)
select a.字段, b.字段
from 表A a
left join 表B b
on a.关联字段 = b.关联字段;
-- 3. 右连接(保留右表全部数据,实际项目少用)
select a.字段, b.字段
from 表A a
right join 表B b
on a.关联字段 = b.关联字段;
避坑:多表联查必须写 on 关联条件,否则产生笛卡尔积(海量无效数据)。
小提示:inner 关键字可以省略,直接写 join,效果一样。
注意:
FROM后面是左表,JOIN后面是右表。LEFT JOIN保留左表全部。RIGHT JOIN保留右表全部。- 不写关联条件会产生笛卡尔积,行数相乘。
- 左连接后,如果
WHERE过滤右表字段,会丢掉左表数据。想保留左表,右表条件写ON。
06 数据增删改
强制规范:update/delete 必须加 where 条件!操作前先用 select 校验条件。
格式规范:字符串、日期加英文单引号,数字、布尔不加。
sql
-- 1. 新增数据
-- 单条全字段新增
insert into 表名 values (值1, 值2, 值3);
-- 指定字段新增(工作常用,灵活)
insert into 表名 (字段1, 字段2) values (值1, 值2);
-- 批量新增(高效)
insert into 表名 (字段1, 字段2) values (值1, 值2), (值3, 值4);
-- 2. 修改数据
update 表名
set 字段1 = 新值, 字段2 = 新值
where 筛选条件;
-- 3. 删除数据
delete from 表名 where 筛选条件;
-- 4. 清空全表数据(两种)
delete from 表名; -- 可回滚、自增id不重置
truncate table 表名; -- 不可回滚、自增id重置
07 高频内置函数
sql
-- 1. 获取当前时间
select now();
-- 获取当前日期(不带时分秒)
select curdate();
-- 2. 提取年/月/日
select year(时间字段), month(时间字段), day(时间字段) from 表名;
-- 3. 字段拼接
select concat(字段1, 分隔符, 字段2) from 表名;
-- 4. 空值兜底(高频!null转为0/默认值)
select ifnull(字段, 默认值) from 表名;
-- 5. 单条件判断 if
select if(条件, '成立值', '不成立值') from 表名;
-- 6. 多条件判断 case when
select
case
when 条件1 then 值1
when 条件2 then 值2
else 默认值
end as 别名
from 表名;
08 高频易错点
- 空值判断:只能用
is null/is not null,禁止用=null(null 是未知值,无法匹配)。 - 分组规则:
select后非聚合字段,必须出现在group by中。 - 去重规则:
distinct是整行去重,多字段需全部相同才会去重。 - 关键字冲突:表名或字段名是关键字,用反引号,如
order,但最好改成orders。 - 金额字段:实际用
DECIMAL,不要用FLOAT。
END