期末速成数据库极简版【查询】(3)

目录

多表查询

【8】多表连接------内连接

🙂等值连接

🙂自然连接

🙂非等值连接

【9】多表连接------外连接

【10】交叉连接不考

【11】联合查询

【12】扩展多表连接

【13】嵌套查询


🙂

多表查询

【8】多表连接------内连接

  • 等值连接(=)(且包含自然连接--两个字段在一张表中)
  • 非等值连接(< > 等)
  • 关于自然连接必须用别名
  • 用别名必须全部都用别名


🙂等值连接

复制代码
--查询选课学生及其选课情况//两张表
--select student.sno , student.sname,scores.grade,scores.course
--from student
--join scores on student.sno = scores.sno


--select student.sno 学号,student.sname 姓名,classes.DEPT
--from student
--join  classes on student.classno=classes.classno
复制代码
--查询--查询选课学生及其选课情况//三个个张表
--select student.sno,sname,ssex,student.classno,dept,scores.course,grade//相同的列需要指定表
--from student
--join scores on student.sno=scores.sno 
--join classes on student.classno=classes.classno


--在上面基础上查询在80以上的同学
--where的写法
--select student.sno,sname,ssex,student.classno,dept,scores.course,grade
--from student,classes,scores
--where student.sno = scores.sno 
--and student.classno=classes.classno
--and grade>80


---取别名的写法
--select s.sno,sname,ssex,s.classno,dept,o.course,grade
--from student s,classes c,scores o
--where s.sno = o.sno 
--and s.classno=c.classno
--and grade>80

🙂自然连接

复制代码
--自然连接
--查询与某某学生同学的同学的学生的学号,姓名和性别student
--select s1.sno,s1.sname,s1.ssex
--from student s1
--join student s2 on s1.sno=s2.sno
--where s2.sno='王曾'

🙂非等值连接

复制代码
--查询比大学英语分数高的学科
--select s1.course,s2.course
--from scores s1
--join scores s2 on s1.course>s2.course
--where s2.course='大学英语'

//查询某个人
//查询全部人数,计算总的。

【9】多表连接------外连接


🙂左外连接🙂右外连接🙂全外连接

复制代码
--查看全部学生的课程情况
--全部学生是主表
--select *
--from student left join scores
--on student.sno=scores.sno

--查看没有课程成绩的学生
--select *
--from student left join scores
--on student.sno=scores.sno
--where scores.grade=null

【10】交叉连接不考

没有实际意义,任意两个表都可以交叉连接

复制代码
select student.sno,sname,grade,course
from scores cross join student
//❌不考

【11】联合查询


复制代码
--select count(sno) from student
--union
--select count(grade) from scores

【12】扩展多表连接


复制代码
--把杨磊的分数加5分
--update scores
--set grade=grade+5
--from student join scores
--on student.sno=scores.sno
--where student.sname='杨磊'

【13】嵌套查询

感谢大家,有补充可以在评论区留言!当然因为我们学校期末考试很水,所以以上这些足够应付期末考试,希望大家可以结合自己的情况好好复习!!

相关推荐
Aloudata3 小时前
人工逐级追查 SQL vs 数据血缘追溯:监管报表异常排查
大数据·数据库·sql
果粒蹬i4 小时前
实时计算平台是什么?工业物联网选型,DolphinDB 拆解
数据库·物联网·实时计算·dolphindb
zxcvb1535 小时前
MySQL三种备份方式:原理、优缺点与实操选型指南
数据库·mysql
夜雪一千5 小时前
PostgreSQL 自定义函数入门
数据库·postgresql
香蕉大香蕉(06敬畏市场)5 小时前
事务隔离级别、MVCC 与锁:一条线串起来
java·服务器·数据库·人工智能·面试·博客
CoderYanger5 小时前
Java EE 进阶:16. Spring原理(第一弹)
java·服务器·数据库·后端·spring·java-ee·github
herinspace5 小时前
管家婆实用帖-SQL 2000如何修改混合模式
服务器·数据库·sql·管家婆软件·合肥管家婆软件·安徽功长网络科技有限公司
镜舟科技6 小时前
一个底座承载四种负载:MirrorData 统一数据底座的存储与计算设计
sql·ai·负载·数据底座·mirrordata·mirrorlake·mirrorbase
霸道流氓气质6 小时前
分布式序列号生成:基于 Redis 原子自增的编号方案
数据库·redis·分布式
Yuzhiyuxia6 小时前
【AI Agent 面试系列】LangGraph 设计思想与基础概念
数据库