当前位置:网站首页>Basic skills: several ways of SQL multi table joint query
Basic skills: several ways of SQL multi table joint query
2022-04-21 14:15:00 【Second brother learns Java】
Preface
Connection query has recently been used in the project , Join query is the most important query in relational data , Including internal connections 、 External connection, etc . Multiple table queries can be implemented through join operators . Let's review .
Text
There are three main types of join queries : Internal connection 、 External connection 、 Cross connect .
Internal connection
Use the comparison operator ( Include =、>、<、<>、>=、<=、!> and !<) Compare tables , Query the data that matches the join condition . Depending on the comparison method used , Inner connection is divided into equivalent connection 、 Natural connection and self connection .
keyword INNER JOIN

Equivalent connection / Equal connection
Use ”=” Relationships are queries that join tables , All the columns in the connected table are listed in the query result , Include the repeating Columns
SELECT
PM_ACT_JOB_RLS.*, PM_ACT_RLS.*
FROM
PM_ACT_JOB_RLS
INNER JOIN PM_ACT_RLS ON PM_ACT_JOB_RLS.RlsPK = PM_ACT_RLS.RlsPK

Natural join
Remove duplicate columns from the equivalent join , Form a connection
SELECT
PM_ACT_JOB_RLS.JobPK,
PM_ACT_RLS.RlsPK,
RlsName
FROM
PM_ACT_JOB_RLS
INNER JOIN PM_ACT_RLS ON PM_ACT_JOB_RLS.RlsPK = PM_ACT_RLS.RlsPK

Self join
If in a join query , The two tables designed are the same table , This kind of query is called self join query .
--c1、c2 Logically, there are two tables , Physically, it's a table
SELECT
c1.CategoryID,
c1.CategoryName
FROM
[dbo].[Category] c1
INNER JOIN [dbo].[Category] c2 ON c1.[CategoryID] = c2.[ParentID]
External connection
The inner connection only returns the data rows that meet the connection conditions , The external connection lists not only the rows that match the connection conditions , Instead, list the left table ( Left outer connection )、 Right table ( Right outer connection ) Or two tables ( When fully externally connected ) All data rows that meet the search criteria in the . External connection is divided into left external connection 、 Right connection 、 There are three types of total external connection .
The left outer join
Returns all rows in the left table , If the row in the left table does not match the row in the right table , The selected fields in the right table of the associated result set are NULL.
keyword LEFT [OUTER] JOIN

-- The left outer join --1679 Data
SELECT
me.*,
mo.*
FROM
MM_LOTS_EXT AS me
LEFT OUTER JOIN MM_LOT_OPERATIONS AS mo ON me.LotID = mo.SerialNumber

Right connection
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 relevant fields in the left table return NULL value .
keyword RIGHT [OUTER] JOIN
-- Right connection --209 Data
SELECT
me.*,
mo.*
FROM
MM_LOTS_EXT AS me
RIGHT OUTER JOIN MM_LOT_OPERATIONS AS mo ON me.LotID = mo.SerialNumber

Full outer join / Complete external connection
Return all recorded data in two connections , Is the union of left outer connection and right outer connection .
keyword FULL [OUTER] JOIN
-- Full outer join --1816 Data
SELECT
me.*,
mo.*
FROM
MM_LOTS_EXT AS me
FULL OUTER JOIN MM_LOT_OPERATIONS AS mo ON me.LotID = mo.SerialNumber
Cross connect / The cartesian product
Cartesian product of two tables , The number of rows in the result set is the product of the number of rows in the two tables .
keyword CROSS JOIN
-- The cartesian product (cross join After the condition can only be used where, Out-of-service on) --45 Data (9*5)
SELECT
*
FROM
PM_ACT_JOB_RLS
CROSS JOIN PM_ACT_RLS
Be careful : with where Conditional clause , It is often a data table that is the product of the number of rows in two tables , Then according to where Conditions choose from .
When the data volume is larger than the price , Cartesian operation will consume the performance of database
summary
Join query is an important symbol in relational database , This time I will review several operations of multi table query with you , Hope to help you !
版权声明
本文为[Second brother learns Java]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211353163039.html
边栏推荐
- redis入门
- I took out 38K from Tencent and showed me the basic ceiling
- Get rid of the messy if else in the project and try the state mode. This is the elegant implementation
- 无穷小的比较
- Definition of Derivative
- 应用打包还是测试团队老大难问题?
- 如何关闭VS Code eslint校验,快来看看吧
- Redis master-slave replication and persistence
- 技术分享 | Selenium 测试用例编写
- Crawler example: crawl Taobao commodity information
猜你喜欢

导数的定义

There are two important limits of the limit existence criterion

ROS2学习笔记(五)-- ROS2命令行操作常用指令总结(一)

Ros2 learning notes (10) -- ros2 launch startup file

【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )

数据仓库架构演变和建设思路

Crawler example: climb the ranking of Chinese Universities

C language selection and circulation classic exercises

录制你的第一个web 自动化测试用例

2021.10.24 程序员(媛)节日快乐!!!
随机推荐
Two days and two nights, 1m pictures are optimized to 100kb
ROS2学习笔记(五)-- ROS2命令行操作常用指令总结(一)
山高路远不畏险
Crawler example: climb the ranking of Chinese Universities
There are two important limits of the limit existence criterion
Redisjson: a redis that can store JSON
初学者3种简单排序的个人总结
顺序表例题个人总结
虫子 二叉树
应用打包还是测试团队老大难问题?
CEPH maintenance command understanding
Ros2 learning notes (8) -- road recognition and debugging based on the application of ros2 parameters
53. Maximum subarray and
C语言分支和循环语句
录制你的第一个web 自动化测试用例
2021.10.24 程序员(媛)节日快乐!!!
MySQL data backup management
二叉树基本操作实现
快速排序几种实现方法及其优化
如何以Sonar为例创建一个适用与所有企业的测试步骤