当前位置:网站首页>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 ?
边栏推荐
- Quick Pow: 如何快速求幂
- koa2学习和使用
- How long will it take to open a mobile account? In addition, is it safe to open a mobile account?
- Analysis of common vlog parameters
- Solr basic operations 13
- Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]
- Solr basic operation 16
- Project 1: deploy lamp ECSHOP e-commerce platform
- Getting started with qpainter: drawing the chess interface
- 请指教同花顺软件究竟是什么?究竟网上开户是否安全么?
猜你喜欢

Preliminary syntax of JS

Common knowledge of ECS security settings

EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended

Construction of module 5 of actual combat Battalion

AI chief architect 9- huxiaoguang, propeller model library and industry application

koa2学习和使用

Getting started with qpainter: drawing the chess interface

西门子低代码平台通过Database Connector 连接Mysql 实现增删改查

JS绘制极坐标颜色渐变
![克隆無向圖[bfs訪問每條邊而不止節點]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆無向圖[bfs訪問每條邊而不止節點]
随机推荐
Exploration and Practice on the future direction of byte cloud database
[advanced C language] dynamic memory management
History of deep learning
About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb
Solr基础操作15
Solr basic operations 12
New CorelDRAW technical suite2022 latest detailed function introduction
漫画安全HIDS、EDR、NDR、XDR
Solr基础操作6
solo 博客皮肤导入 skins 文件夹后出现 500 错误
QT learning 03 birth and essence of QT
Getting started with qpainter: drawing the chess interface
Clone undirected graph [bfs accesses each edge but not only nodes]
项目一:部署 LAMP ecshop电商平台
Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
Summary of DOM knowledge points
gyctf_ 2020_ document
The role of VMware virtual machine
Leetcode (680) -- verifying palindrome string II
modelsim的TCL脚本的define incdir命令解析