mysql多表联查

目录

表关系:

1.多表联查

sql 复制代码
-- 联查两张表

-- 学生和班级信息  -- 数据不一致问题
-- 笛卡尔积
select * from student,class

-- 面试题:内联查询()和外联查询有什莫区别?

-- 等值联查 (内联查询,隐式内连接)   -- 笛卡尔积  表多数 行数量少
select * from student,class 
where student.classid = class.classid

-- inner join on(显式内连接)      -- 表少 行数数量多
select * from  student  
inner join class on student.classid = class.classid


-- 外联查询  -- 主查表的数据全部出现,从表关系条件进行拼接
-- 左外联  
-- left join  on 
select * from  class
left join  student on student.classid = class.classid

-- 右外联
-- right join on 
select * from class 
right join student on student.classid = class.classid


-- 没有班级的学生
select * from class 
right join student on student.classid = class.classid
where class.classid is null

-- 没有学生的班级
select * from  class
left join  student on student.classid = class.classid
where student.sid is null


-- union 并集 , 全链接查询, 去除重复的数据,效率低下
select * from class 
right join student on student.classid = class.classid
union
select * from  class
left join  student on student.classid = class.classid

-- union all 不去重 ,效率高
select * from class 
right join student on student.classid = class.classid
union all
select * from  class
left join  student on student.classid = class.classid

2.子查询

sql 复制代码
-- 子查询

-- 查询id最大的一个学生(使用排序+分页实现)
select * from student order by sid desc limit 1;

-- where 子查询
select * from student 
where sid = (select max(sid) from student);


-- 查询每个班下id最大的学生(使用where子查询实现)

select * from student where sid in (
	select max(sid) from student group by classid
)


-- 查询大于5人的班级名称和人数(不使用子查询)
select classname,count(*) from student,class 
where student.classid = class.classid 
group by class.classid 
having count(*) > 5

-- from 子查询
select classname,人数 from class, 
(select classid,count(*) 人数 
	from student group by classid) t1
where  t1.classid = class.classid and 人数 > 5

-- exists 子句有结果父句执行,子句没结果父句不执行
select * from teacher where exists(
	select * from student where ssex = '外星人'
)



-- some/any  all 

-- some 和 any 同一个作用的


-- 题:查询出一班成绩比二班最低成绩高的学生
select DISTINCT student.* from sc 
inner join student on sc.sid = student.sid
where classid = 1
and score > (
	select min(score) from sc 
	inner join student on sc.sid = student.sid
	where classid = 2
);


select score from sc 
	inner join student on sc.sid = student.sid
	where classid = 2

70.0
60.0
80.0
50.0
30.0
20.0
31.0
34.0


select DISTINCT student.* from sc 
inner join student on sc.sid = student.sid
where classid = 1
and (
	score > 70.0 or score > 60.0 or score > 80.0 
	or score >50 or score > 30 or score > 20
	or score > 31 or score > 34
)

select DISTINCT student.* from sc 
inner join student on sc.sid = student.sid
where classid = 1 and score > any (
	select score from sc 
		inner join student on sc.sid = student.sid
		where classid = 2
)


-- 题:查询出一班成绩比二班最高成绩高的学生

select DISTINCT student.* from sc 
inner join student on sc.sid = student.sid
where classid = 1
and score > (
	select max(score) from sc 
	inner join student on sc.sid = student.sid
	where classid = 2
);


select DISTINCT student.* from sc 
inner join student on sc.sid = student.sid
where classid = 1
and (
	score > 70.0 and score > 60.0 and score > 80.0 
	and score >50 and score > 30 and score > 20
	and score > 31 and score > 34
)

select DISTINCT student.* from sc 
inner join student on sc.sid = student.sid
where classid = 1 and score > all (
	select score from sc 
		inner join student on sc.sid = student.sid
		where classid = 2
)

3.逻辑语句

sql 复制代码
-- 逻辑语句
-- IF(expr1,expr2,expr3)
select * from teacher
select tid,tname,if(tsex=1,'男','女') tsex,tbirthday,taddress from teacher;

-- IFNULL(expr1,expr2)
-- expr1 字段
-- expr2 当字段为Null,要显示的默认值
select sid,sname,IFNULL(birthday,'这个学生没有生日') from student;

-- 搜索case
select tid,tname,
case tsex
	when 1 then '男'
	when 0 then '女'
	else  '外星人'
end  as tsex,
tbirthday from teacher;

-- 简单case
select tid,tname,tsex,
case
	when tsex>1 then '外星人'
	when tsex=1 then '男'
	when tsex<1 then '女'
end as tsex,
tbirthday from teacher;

select sid,cid,
case
	when score > 90 then 'A'
	when score > 80 then 'B'
	when score > 70 then 'C'
	when score > 60 then 'D'
	when score < 60 then '不及格'
end as score
from sc;

select sid,cid,
case
	when score > 90 then 'A'
	when score > 80 then 'B'
	when score > 70 then 'C'
	when score > 60 then 'D'
	when score < 60 then '不及格'
end as score
from sc;
相关推荐
一条泥憨鱼几秒前
【Redis | 黑马点评】基于Redis的消息队列 | stream消息队列 源码解读
java·数据库·redis·缓存·lua
CodeGL1 分钟前
9、Python基础之IO编程
开发语言·数据库·python
每天一道题2 分钟前
Redis 缓存为什么会出问题?穿透、击穿、雪崩与缓存一致性
数据库·redis·缓存
tongyiixiaohuang25 分钟前
物料成本核算单同步返回信息策略实战教程:从金蝶云星空回写 MySQL 的闭环设计
数据库·mysql
星云低代码开发平台30 分钟前
AI 工作台记录删除与业务审计留存:三层数据边界设计
数据库·人工智能·架构
深度学习040738 分钟前
MySQL InnoDB Cluster 部署完整文档
android·mysql·adb
code_slave(码畜)1 小时前
微服务架构落地:分库分表实战(下) —— 分布式主键、跨分片查询与分页聚合防坑
数据库·spring boot·分布式·spring cloud·微服务·架构·分库分表
dishugj1 小时前
https://blog.csdn.net/dishugj/article/details/162068579
数据库·系统架构
时间售卖铺1 小时前
MySQL 安装部署
运维·数据库·mysql·云原生
泡海椒1 小时前
JQuick-SQL 子查询:WHERE、SELECT、HAVING 与派生表实践
数据库·sql