常见SQL整理

基础语法

查询

全表查询
sql 复制代码
select * from student
选择查询
sql 复制代码
select name,age from student
别名
sql 复制代码
select name as 学生姓名,age as 学生年龄 from student
常量和运算
sql 复制代码
select name,score,score*2 as double_score from student
条件查询--where
sql 复制代码
select name,score from student where name='鱼皮'
条件查询--运算符
sql 复制代码
select name,age from student where name != '热dog'
条件查询--空值
sql 复制代码
select name,age,score from student where age is not null
条件查询--模糊查询
sql 复制代码
select name,score from student where name not like '%李%'
条件查询--逻辑运算
sql 复制代码
select name,score from student where name like '%李%' or score>500
去重
sql 复制代码
select distinct class_id,exam_num from student
排序
sql 复制代码
select name,age,score from student order by score desc, age asc
截断和便宜
sql 复制代码
select name,age from student order by age limit 1,3
条件分支
sql 复制代码
select name, case when (age>60) then '老同学' when (age>20) then '年轻' else '小同学' end as age_level from student order by name
时间函数
sql 复制代码
select name, date() as 当前日期 from student
字符串处理
sql 复制代码
select id,name,upper(name) as upper_name from student where name='热dog'
聚合函数
sql 复制代码
select sum(score) as total_score,avg(score) as avg_score,max(score) as max_score,min(score) as min_score from student
单字段分组
sql 复制代码
select class_id,avg(score) as avg_score from student group by class_id
多字段分组
sql 复制代码
select class_id,exam_num,count('*') as total_num from student group by class_id,exam_num
having子句
sql 复制代码
select class_id,sum(score) as total_score from student group by class_id having total_score>150
关联查询--cross join
sql 复制代码
select s.name as student_name, s.age as student_age, c.id as class_id, c.name as class_name from student as s cross join class as c
关联查询--inner join
sql 复制代码
select s.name as student_name,s.age as student_age,c.id as class_id,c.name as class_name,c.level as class_level from student as s inner join class as c on s.class_id = c.id
关联查询--outer join
sql 复制代码
select s.name as student_name,s.age as student_age,c.id as class_id,c.name as class_name,c.level as class_level from student as s left join class as c on s.class_id = c.id
子查询
sql 复制代码
select name,score,class_id from student where class_id in (
    select distinct id from class
)
子查询exists
sql 复制代码
select name,age,class_id from student where not exists (
    select 1 from class where student.class_id=class.id
)
聚合查询
sql 复制代码
select name,age,score,class_id from student
union all
select name,age,score,class_id from student_new
相关推荐
CoderYanger1 小时前
Java EE 进阶:8.MyBatis 操作数据库(入门)(第二弹)
java·数据库·spring boot·spring·面试·java-ee·mybatis
wafrdggh4 小时前
2026电商快递批量查询怎么用?固乔助手模板设置评测
大数据·数据库·小程序
一个天蝎座 白勺 程序猿5 小时前
KingbaseES数据库归档重做日志文件管理:从配置到运维的实战指南
运维·数据库·oracle·kingbasees·金仓数据库
龙腾AI白云8 小时前
仿真到现实(Sim-to-Real)迁移:跨越仿真与现实鸿沟
数据库·机器学习·plotly·知识图谱·tornado
freejackman10 小时前
【实战二】字段级血缘追踪:拆解 AST 迷局,精准穿透函数链与子查询
大数据·数据库·sql
.痞老板.11 小时前
mysql初阶--增删改查操作
数据库·mysql
lambo mercy17 小时前
AI智能租房助手
数据库
浮生若城18 小时前
MySQL:库与表的操作
数据库·mysql
薛定谔的悦18 小时前
光储充一体机从主控到执行单元的双层架构
数据库·mongodb·架构
数字新视界18 小时前
动环监控信创适配在智能机房管理中的应用价值与未来展望
大数据·服务器·数据库·动环监控系统·动力与环境监控系统