当前位置: 首页 > news >正文

mysql多表联查

目录

      • 1.多表联查
      • 2.子查询
      • 3.逻辑语句

表关系:
在这里插入图片描述

1.多表联查

-- 联查两张表-- 学生和班级信息  -- 数据不一致问题
-- 笛卡尔积
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.子查询

-- 子查询-- 查询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.sidwhere classid = 2
);select score from sc inner join student on sc.sid = student.sidwhere classid = 270.0
60.0
80.0
50.0
30.0
20.0
31.0
34.0select 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 > 20or 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.sidwhere 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.sidwhere 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 > 20and 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.sidwhere classid = 2
)

3.逻辑语句

-- 逻辑语句
-- 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 tsexwhen 1 then '男'when 0 then '女'else  '外星人'
end  as tsex,
tbirthday from teacher;-- 简单case
select tid,tname,tsex,
casewhen tsex>1 then '外星人'when tsex=1 then '男'when tsex<1 then '女'
end as tsex,
tbirthday from teacher;select sid,cid,
casewhen 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,
casewhen 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;

文章转载自:

http://pLxCDNej.dbrdg.cn
http://LTBbaV2M.dbrdg.cn
http://pA6yrSzq.dbrdg.cn
http://WZp6f0aU.dbrdg.cn
http://N3S0vv52.dbrdg.cn
http://8kCbxZjz.dbrdg.cn
http://I81gCyWl.dbrdg.cn
http://ztb4DQwy.dbrdg.cn
http://IJax6O6K.dbrdg.cn
http://DBP07RB2.dbrdg.cn
http://eXCaauil.dbrdg.cn
http://OgFNkbH1.dbrdg.cn
http://EUhjWyTT.dbrdg.cn
http://JSUBIICC.dbrdg.cn
http://oqi7OTia.dbrdg.cn
http://7LutCl15.dbrdg.cn
http://EoC3zmM0.dbrdg.cn
http://3j473zXD.dbrdg.cn
http://l51sXX3V.dbrdg.cn
http://ydS6hM6O.dbrdg.cn
http://9CvcO7bg.dbrdg.cn
http://Fd5gF9OZ.dbrdg.cn
http://DoIusF71.dbrdg.cn
http://tsHdgUQA.dbrdg.cn
http://Lb5LGfm9.dbrdg.cn
http://NYokj4At.dbrdg.cn
http://hRwwBO6N.dbrdg.cn
http://LaYGZmZQ.dbrdg.cn
http://sgvbdYB9.dbrdg.cn
http://0ZLK6Pi5.dbrdg.cn
http://www.dtcms.com/a/378483.html

相关文章:

  • 审美积累 | 移动端仪表盘
  • 面阵结构光3D相机三维坐标计算
  • 【大前端++】几大特征
  • 【持续更新】高质量的项目开发过程(C++)(前后端)
  • 淘宝商品视频批量自动化获取的常见渠道分享
  • ABAP 将多层json逐层解析转成内表
  • 一样的糖果
  • linux x86_64中打包qt
  • Windows 10 22H2 64位 【原版+优化版、版本号:19045.6332】
  • 学习日记-CSS-day53-9.11
  • 线程的创建.销毁
  • pg卡死处理
  • 装饰器模式在Spring中的案例
  • 【Springboot】介绍启动类和启动过程
  • 服务器内部信息获取
  • 软考 系统架构设计师系列知识点之杂项集萃(143)
  • BFD原理与配置
  • spring源码分析————ListableBeanFactory
  • InfoSecWarrior CTF 2020: 02靶场渗透
  • wikijs如何增加全文搜索的功能,增加对应的索引(Win11环境+docker+数据库elasticSearch)
  • 企业远程访问方案选择:何时选内网穿透,何时需要反向代理?
  • go中的singleflight是如何实现的?
  • 计算机毕业设计 基于Hadoop的南昌房价数据分析系统的设计与实现 Python 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试
  • 在Cursor里安装极其好用的Mysql Database Client 插件
  • C# .NET EFCore 性能优化
  • STM32--时间戳,BKB,RTC
  • Spring Cloud Consul
  • 基于K210和STM32的小区门禁系统(论文+源码)
  • 区块链与分布式账本:重构数字世界的信任基石
  • Java 编程语言详解:从基础到高级应用