当前位置:网站首页>Database hyperphone (II)
Database hyperphone (II)
2022-07-27 17:34:00 【Moon in bubble】
Multi table join query
One 、 Join query structure
1.where、inner join
where The join statement used in clause , In database language , It's called implicit connection .inner join……on The join produced by a clause is called an explicit join .( other join Parameters are also explicit connections )
where and inner join The resulting connection , There is no essential difference , It turns out the same thing .
If two sides are not equal , Then it cannot be connected
2. Specific internal and external connections
- (inner) join: Internal connection , Understood as a “ Connect effectively ”, The data in both tables will be displayed
- left(outer) join:( Left ) External connection , Display qualified data rows , And the unqualified data rows in the left table . At this time, the right data row will be easy NULL fill
- right(outer) join:( Right ) External connection , Display qualified data rows , And the unqualified data rows in the right table . At this time, the left data row will be easy NULL fill
- full(outer)join: Display qualified data rows , And the data rows of the left table and the right table that do not meet the conditions . At this time, data lines that lack data will be easy NULL fill
- cross join: No special requirements , Any table can be cross queried
3. Use AS Precautions for
< Source table name > [ AS ] < Table alias >
Be careful : When an alias is specified for a table , Elsewhere in the query statement , Aliases should be used wherever table names are used , Instead of using the original table name .
4. About self connection query
- Special inner connection
- Interconnected tables are physically the same table
- You must alias two tables , Make it logically two tables .
Such as :
FROM surface 1 AS T1 – Generate... In memory “T1”
JOIN surface 1 AS T2 – Generate... In memory “T2”
Two 、 Internal connection query (inner)join
(1) Inquire the course number taught by Mr. Liu Wei , List the teacher number , Teacher's name , Course no. (3 Methods )
① select Tno,TN,Cno
from T,TC
where T.Tno = TC.Tno and TN=' Liu Wei '
②select Tno,TN,Cno
form T join TC on T.Tno=TC.Tno
where TN=' Liu Wei '
③select R1.Tno,R1.TN,R2.Cno
from (select Tno,Cno from TC) as R1
inner join (select Tno,TN from T where TN = ' Liu Wei ') as R2
on R1.Tno=R2.Tno
(2) Inquire about ‘ Information management department ’ Repaired ‘ Fundamentals of Computer Culture ’ Names and grades of students
select S.Sn,scorce
from S
join SC on S.Sno = SC.Sno
join C on C.Cno
where dept = ' Information management department ' and CN =' Fundamentals of Computer Culture '
(3) Count the number of students taking elective courses in each age group
select age,count(distinct sno) # duplicate removal
from S,SC
where S.Sno = SC.Sno
group by age
3、 ... and 、 External connection query
(1) Check the student numbers of all the students , full name , Course name and grade ( The course selection information of students without course selection is empty )
select Sno,Sn,Cn,Scorce
from S
left join SC on S.Sno = SC.Sno
left join C on C.Cno=SC.Cno
(2) Query the course name of unattended courses
①select CN
from C left join SC on C.Cno = SC.Cno
where SC.Cno is null # where SC.Sno is null It's fine too
②select CN
from C where
Cno not in (select Cno from SC )
Four 、 Cross query ( No special requirements , Any table can be cross queried )
Cross check the student schedule and curriculum
select * from S cross join C
5、 ... and 、 Self connect query
(1) Look up all of them “ Liu Wei ” The name of the teacher who works high 、 Wages 、 And Liu Wei's salary (3 Kind of )
①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 = ' Liu Wei '
②select X.TN,X.Sal,Y.Sal
from T as X inner join T as Y
on X.Sal >Y.Sal and Y.TN = ’ Liu Wei
③select R1.TN,R1.Sal,R2.Sal
from (select TN,Sal from T) as R1
inner join
(select Sal from T where TN =‘ Liu Wei ') as R2 on R1.Sal <font color = 'red'> > R2.Sal # Greater than connection
(2) Retrieve all student names , Age and course name (2 Kind of )
①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 # from S Student number of watch handle , full name , Choose your age , Put in R3
inner join
(select R2.Sno,R1.CN # from C,SC Find out the student number of the student in the class , Name of the selected course , Put it in 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 Connect , Retrieve all student names , Age and course name
(3) And Liu Chen are students in the same department , Department
select S2.Sn,S2.dept
from S as S1
inner join S as S2
on S1.dept = S2.dept
where S1.SN = ' Liu Chen '
and S2.SN <font color='red'>< ></font> ' Liu Chen '
Be careful : Alias must be used here
边栏推荐
- 立创EDA——原理图的布局与检查(三)
- day07 作业
- Mobile end Foundation
- Kubernetes Part 7: using kubernetes to deploy prometheus+grafana monitoring system (kubernetes work practice class)
- About paths mapping in SAP ui5 application ui5.yaml
- Swift QQ授权登录 坑集
- Swift QQ authorized login pit set
- 大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
- Kubernetes第七篇:使用kubernetes部署prometheus+grafana监控系统(Kubernetes工作实践类)
- 国产新冠口服药为什么是治艾滋病的药
猜你喜欢
随机推荐
成本高、落地难、见效慢,开源安全怎么办?
数据库超话(三)
科目三: 直线行驶
Built in object (bottom)
每条你收藏的资讯背后,都离不开TA
交换机和路由器技术-02-以太网交换机工作原理
立创EDA——原理图的布局与检查(三)
.net core with microservices - what is a microservice
Oracle-Linux-7.9是否能支持Oracle-19c的ACFS文件系统?
Day 7 summary & homework
VO、DO、DTO、PO是什么
SVM+Surf+K-means花朵分类(Matlab)
Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
Subject 3: straight driving
Chen Yili of ICT Institute: reducing cost and increasing efficiency is the greatest value of cloud native applications
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
Sharing of local file upload technology of SAP ui5 fileuploader
MySQL: 函数
Explain the idempotence of distributed system in detail
渐变环形进度条









