当前位置:网站首页>Connection query of SQL Server database
Connection query of SQL Server database
2022-06-30 00:15:00 【Little brother】
I learned it today SQL Server Connection query for , Let's record .
What is connection query ?
Query multiple tables at the same time , Query with internal connection , External connection query , Cross query . External connection query is divided into left external connection query 、 Right outer connection query and all outer connection query , However, a join query can also query a table called a self join query .
INNER JOIN: Indicates internal connection ,INNER You can omit and write only JOIN
ON: Give the connection conditions
(JOIN ... ON ...) Equate to (,... where...)
Prepare first 2 A table to query Table01,Table02:
Table01

Table02

that , First come Let's talk about the inner join query of multiple tables
select Table01.SID ,SName,CID,CContact,CPhone
from Table01 JOIN Table02
ON Table01.SID=Table02.SID
In the multi table join query, if there are the same fields in the joined tables , The table prefix must be added before the reference ( Inquire about Table01 Medium SID Just write Table01.SID), If the query field is unique in multiple tables , The table name prefix can be omitted .
Query results :

next Let's talk about the self join query for querying a table , In fact, it can be regarded as an alternative inner connection .
A self join query must specify two aliases for the table , In fact, it is to copy one's own table , Make it two tables .
select S1.SID,S1.SName,S1.SDID,s1.SPostID
from Table01 AS S1 JOIN Table01 AS S2
ON S1.SDID!=S2.SDID and S1.SPostID=S2.SPostID
Table Define an alias S1 and S2
Query results :

Let's talk about external link query , There are left join queries (LEFT OUTER JOIN), Right connection query (RIGHT OUTER JOIN) And full connection query (FULL OUTER JOIN). OUTER It can be omitted
Prepare first 2 A watch ,Table01,Table02:
Table01 Table02

1、 Left outer join query is also called left join query (LEFT OUTER JOIN)
Concept : Returns all rows in the left table , If the row in the left table does not match the row in the right table , The corresponding behavior in the right table is null .
select * from Table01 left join Table02
on Table01.classId = Table02.classId
Query results :

2、 Right outer join query is also called right join query (RIGHT OUTER JOIN)
Concept : The opposite of the left link , Returns all the rows in the right table , If the row in the right table does not match the row in the left table , The corresponding behavior in the left table is null .
select * from Table01 right join Table02
on Table01.classId = Table02.classId
Query results :
3、 Full external connection query is also called full connection query (FULL OUTER JOIN)
Concept : Returns all rows in the left and right tables . When a row has no matching row in another table , The other table is null ( First, check the right table according to the left table , If we can't find out , The behavior of the right table is null , After checking, the rest of the right table will be displayed , All rows in the left table are null )
select * from Table01 full join Table02
on Table01.classId = Table02.classId
Query results :
The last one is cross query (CROSS JOIN) Also called The cartesian product , The query table uses two tables with external links Table01,Table02
Concept : No WHERE Conditional clause , It will return the... Of the two connected tables The cartesian product , That is, each row of the left table is combined with each row of the right table , The number of rows returned is equal to the product of the number of rows in two tables ( for example :Table01t and Table02, return 4*4=16 Bar record ), If belt where, Returns or displays the number of matching rows .
1、 No WHERE:
select * from Table01 cross join Table02 Or you could write it as select * from Table01,Table02
Query results :

2、 belt WHERE Clause , First the 2 Table product combination , Then query according to the conditions
notes : Cross band conditional query must use WHERE, Out of commission ON
select * from Table01 CROSS join Table02
WHERE Table01.classId = Table02.classId
Query results :

Finally, I have finished writing , That's all for connection query , After reading it, do you think it's very simple ?
边栏推荐
- How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
- Cacti maximum monitoring number test
- Solr basic operations 14
- Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
- Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
- Solr基础操作13
- 蛇形矩阵(数组模拟方向, d代表转弯)
- 多数元素II[求众数类之摩尔投票法]
- 西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
- Solr基础操作12
猜你喜欢

MySQL multi table query

QT learning 05 introduction to QT creator project

Summarize Flink runtime architecture in simple terms

自动融合,驰骋海外丨跨境电商YescomUSA携手云扩实现一站式自动化服务

由GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable}引起的刨根问底

剑指 Offer II 037. 小行星碰撞
![克隆無向圖[bfs訪問每條邊而不止節點]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆無向圖[bfs訪問每條邊而不止節點]

The role of VMware virtual machine

蛇形矩阵(数组模拟方向, d代表转弯)

Cartoon security HIDS, EDR, NDR, XDR
随机推荐
蛇形矩阵(数组模拟方向, d代表转弯)
Andorid source build/envsetup. SH details to know
Fine grained identification, classification, retrieval data set collation
AI chief architect 9- huxiaoguang, propeller model library and industry application
golang7_ TCP programming
Activity invitation | the Apache Doris community essay and speech solicitation activity has begun!
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
漫画安全HIDS、EDR、NDR、XDR
333333333333333333333333333333
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
Some specifications based on zfoo development project
利用 CertBot 申请 Let’s Encrypt SSL 证书
Finding a job in 2022 is the "last lesson" for graduates
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
西门子低代码 9.14版本: 满足不同需求
旋转彩色三叶草
Solr basic operation 6
Embedded development: Hardware in the loop testing
vlog常用参数解析
01背包问题