常见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
相关推荐
云飞云共享云桌面几秒前
SolidWorks如何实现多人共享
服务器·前端·数据库·人工智能·3d
正在走向自律2 分钟前
金仓数据库:Oracle迁移背景下的兼容性挑战与迁移成本深度解析
数据库·oracle·国产数据库·电科金仓
AORUO奥偌4 分钟前
医用气体报警箱:构筑楼层气体安全的监测前哨
大数据·数据库
醒过来摸鱼9 分钟前
redis源码deps目录
数据库·redis·缓存
北辰当尹14 分钟前
【小迪安全2023】day43 php应用&SQL注入&符号拼接&请求方法&HTTP头&JSON&编码类
sql·安全·php
Huanlis16 分钟前
Redis Stream 核心原理与实战指南
数据库·redis·缓存
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.17 分钟前
HAProxy四层负载实战:MariaDB高可用方案
数据库·mariadb
xixi_66619 分钟前
mysql 的分组函数 ROLLUP 语法
数据库·mysql
范纹杉想快点毕业23 分钟前
嵌入式通信协议深度解析:从SPI/I2C到CAN总线的完整实现指南嵌入式工程师的炼成之路:从校园到实战的跨越
linux·运维·服务器·数据库·算法
数据知道25 分钟前
PostgreSQL 实战:如何优雅高效地进行全文检索
大数据·数据库·postgresql·全文检索