常见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
相关推荐
程序新视界11 分钟前
MySQL中,IS NULL和IS NOT NULL不会走索引?错!
数据库·mysql·dba
wdfk_prog25 分钟前
闹钟定时器(Alarm Timer)初始化:构建可挂起的定时器基础框架
java·linux·数据库
许长安33 分钟前
Redis(二)——Redis协议与异步方式
数据库·redis·junit
java_python源码1 小时前
python高校心理健康服务小程序(源码+文档+调试+基础修改+答疑)
数据库·sqlite
简色1 小时前
题库批量(文件)导入的全链路优化实践
java·数据库·mysql·mybatis·java-rabbitmq
点灯小铭2 小时前
基于单片机的自动存包柜设计
数据库·单片机·mongodb·毕业设计·课程设计
失散132 小时前
软件设计师——09 数据库技术基础
数据库·软考·软件设计师
养生技术人3 小时前
Oracle OCP认证考试题目详解082系列第53题
数据库·sql·oracle·database·开闭原则·ocp
银帅183350309713 小时前
2018年下半年试题四:论NoSQL数据库技术及其应用
数据库·架构·nosql
liu****3 小时前
基于websocket的多用户网页五子棋(九)
服务器·网络·数据库·c++·websocket·网络协议·个人开发