当前位置:网站首页>数据库超话(二)
数据库超话(二)
2022-07-27 15:28:00 【泡泡里的月亮】
多表连接查询
一、连接查询结构
1.where、inner join
where子句中使用的连接语句,在数据库语言中,被称为隐性连接。inner join……on子句产生的连接称为显性连接。(其他join参数也是显性连接)
where和inner join产生的连接关系,没有本质区别,结果也一样。
若两边不相等,则连接不上
2.具体内外连接
- (inner) join:内连接,理解为“有效连接”,两张表中都有的数据才会显示
- left(outer) join:(左)外连接,显示符合条件的数据行,以及左边表不符合条件的数据行。此时右数据行会易NULL填充
- right(outer) join:(右)外连接,显示符合条件的数据行,以及右边表不符合条件的数据行。此时左数据行会易NULL填充
- full(outer)join:显示符合条件的数据行,以及左边表和右边表不符合条件的数据行。此时缺乏数据的数据行会易NULL填充
- cross join:无特殊要求,任何表都可以交叉查询
3.使用 AS的注意事项
<源表名> [ AS ] <表别名>
注意:当为表指定了别名时,在查询语句中的其他地方,所有用到表名的地方都要使用别名,而不能再使用原表名。
4.关于自连接查询
- 特殊的内连接
- 相互连接的表物理上为同一张表
- 必须为两个表取别名,使之在逻辑上成为两个表。
如:
FROM 表1 AS T1 – 在内存中生成“T1”
JOIN 表1 AS T2 – 在内存中生成“T2”
二、内连接查询(inner)join
(1)查询刘伟老师所教授的课程号,列出教师号,教师姓名,课程号(3种方法)
① select Tno,TN,Cno
from T,TC
where T.Tno = TC.Tno and TN='刘伟'
②select Tno,TN,Cno
form T join TC on T.Tno=TC.Tno
where TN='刘伟'
③select R1.Tno,R1.TN,R2.Cno
from (select Tno,Cno from TC) as R1
inner join (select Tno,TN from T where TN = '刘伟') as R2
on R1.Tno=R2.Tno
(2)查询‘信息管理系’修了‘计算机文化基础’的学生姓名和成绩
select S.Sn,scorce
from S
join SC on S.Sno = SC.Sno
join C on C.Cno
where dept = '信息管理系' and CN ='计算机文化基础'
(3)统计每一年龄组选修课程的学生人数
select age,count(distinct sno) # 去重
from S,SC
where S.Sno = SC.Sno
group by age
三、外连接查询
(1)查询所有学生的学号,姓名,选课名称及成绩(没有选课的同学的选课信息显示为空)
select Sno,Sn,Cn,Scorce
from S
left join SC on S.Sno = SC.Sno
left join C on C.Cno=SC.Cno
(2)查询无人选课的课程名
①select CN
from C left join SC on C.Cno = SC.Cno
where SC.Cno is null # where SC.Sno is null 也可以
②select CN
from C where
Cno not in (select Cno from SC )
四、交叉查询(无特殊要求,任何表都可以交叉查询)
对学生表和课程表进行交叉查询
select * from S cross join C
五、自连接查询
(1)查询所有比“刘伟”工作高的教师姓名、工资、和刘伟的工资(3种)
①select X.TN,X.Sal as Sal_a,Y.Sal as Sal_b
from T as X ,T as Y
where X.sal >Y.sal and Y.TN = '刘伟'
②select X.TN,X.Sal,Y.Sal
from T as X inner join T as Y
on X.Sal >Y.Sal and Y.TN = ’刘伟
③select R1.TN,R1.Sal,R2.Sal
from (select TN,Sal from T) as R1
inner join
(select Sal from T where TN =‘刘伟') as R2 on R1.Sal <font color = 'red'> > R2.Sal #大于连接
(2)检索所有学生姓名,年龄和选课名称(2种)
①select SN,Age,CN
from S,SC ,C
where S.Sno = SC.Sno and SC.Cno = C,.Cno
②select
from
(select Sno,SN,Age from S) as R3 #从S表把学号,姓名,年龄选出来,放入R3
inner join
(select R2.Sno,R1.CN #从C,SC查出学课学生的学号,被选的课程名,放到R4
from
(select Cno,Cn from C ) as R1
inner join
(select Sno,Cno from SC) as R2
on R1.Cno = R2.Co) as R4
on R3.Sno = R4.Sno #R3,R4连接,检索所有学生姓名,年龄和选课名称
(3)与刘晨是一个系学习的学生姓名,所在系
select S2.Sn,S2.dept
from S as S1
inner join S as S2
on S1.dept = S2.dept
where S1.SN = '刘晨'
and S2.SN <font color='red'>< ></font> '刘晨'
注意:这里必须使用别名
边栏推荐
- 今日睡眠质量记录82分
- CUE语言基础入门:CUE是一门为配置而生的语言
- Global string object (function type) +math object
- Following the example of IE, is the decline of Firefox inevitable?
- 三表联查3
- 信通院陈屹力:降本增效是云原生应用最大价值
- Gartner 权威预测未来4年网络安全的8大发展趋势
- Bit band operation of semaphore protection
- Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
- Oracle-Linux-7.9是否能支持Oracle-19c的ACFS文件系统?
猜你喜欢

大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?

信号量保护之位带操作

记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析

App crash collection and analysis

Technical practice dry goods | from workflow to workflow

The chess robot broke the finger of a 7-year-old boy. Netizen: it violated the first law of robots

格力「不清凉」:巨头诉讼落幕又遭大经销商减持,空调新战场还晚人一步?

Kubernetes第七篇:使用kubernetes部署prometheus+grafana监控系统(Kubernetes工作实践类)

File class byte input and output stream

通过 FileUploader 的初始化,了解 SAP UI5 应用的 StaticArea 初始化逻辑
随机推荐
Purchase in Appstore
小于n的最大数
高精度定时器
Project exercise: the function of checking and modifying tables
KMP模板——字符串匹配
SAP UI5 FileUploader 的隐藏 iframe 设计明细
信号量保护之位带操作
Mobile end Foundation
Shell programming specifications and variables
Flex flex flex box layout
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
Kubernetes Chapter 8: deploy NFS system with kubernetes to complete database persistence (kubernetes work practice class)
WebView basic use
Functions in JS
Passive income: return to the original and safe two ways to earn
Gartner authority predicts eight development trends of network security in the next four years
第7天总结&作业
This large model sparse training method with high accuracy and low resource consumption has been found by Alibaba cloud scientists! Has been included in IJCAI
Node package depends on download management
成本高、落地难、见效慢,开源安全怎么办?