Understand inner joins, left outer joins, and right outer joins of sql statements
Recommended (free): SQL Tutorial
When you first learn the database, do you have some doubts about the connection query in DQL? Do you know when and what kind of connection query should be used in what scenario?
Don’t worry, let me introduce to you my understanding of the characteristics and application scenarios of inner joins, left outer joins, and right outer joins for your reference.
The following code demonstrations are based on the name table and country table.
name table
id | name |
---|---|
1 | 西士 |
2 | Yang Yuhuan |
3 | Diao Chan |
4 | Wang Zhaojun |
Zhao Feiyan |
country table
country | A_ID | |
---|---|---|
Yue people during the Spring and Autumn Period | 1 | |
A native of Yongle, Puzhou in the Tang Dynasty | 2 | |
A native of Xinzhou, Shanxi in the late Eastern Han Dynasty | 3 | |
People from Zigui, Nanjun in the Western Han Dynasty | 4 | |
Warring States Period | 6 |
1) There is a relationship between b table A_ID and a table a_id1) Inner join2) The connection query uses association conditions to remove mismatches Otherwise, the data will have a Cartesian product
语法: select 要查询的字段 from 表名1 inner join 表名2 on 表1.字段 = 表2.字段;
inner join 可简写为 逗号,
内连接特点:
只会保留完全符合on后条件的数据
应用场景:
如果两张表有外键关系可以使用内链接,因为通过内链接每一条只能返回单条记录
select * from name n inner join country c on n.id = c.N_ID;
name | id | country | N_ID | |
---|---|---|---|---|
1 | 元国人 | 1 | 2 | |
2 | People from Yongle, Puzhou, Tang Dynasty | 2 | ##3 | |
3 | Eastern Han Dynasty A native of Xinzhou, Shanxi in the last years | 3 | 4 | |
4 | A native of Zigui, Nanjun during the Western Han Dynasty | 4 |
语法: select 要查询的字段 from 表1 left outer join 表2 on 表1.字段 = 表2.字段; outer 可省略 左外连接特点: 以左表为主,会保留左表中不符合on后条件的数据 应用场景: 只有部分记录可以从表2中查到,但表1想要显示所有记录,就可以和表2通过左外连接查询。
id
id | country | N_ID | ||
---|---|---|---|---|
1 | 元国人 | 1 | 2 | |
2 | People from Yongle, Puzhou, Tang Dynasty | 2 | ##3 | Diao Chan |
People from Xinzhou, Shanxi Province in the late Eastern Han Dynasty | 3 | ##4 | Wang Zhaojun | |
南君 Zigui people during the Western Han Dynasty | 4 | 5 | Zhao Feiyan | |
null | null | 3) Right outer join |
id
namecountry | N_ID | ##1 | 西士 | |
---|---|---|---|---|
1 | ##2 | Yang Yuhuan | 2 | |
2 | ##3 | Diao Chan | 3 | People from Xinzhou, Shanxi in the late Eastern Han Dynasty |
##4 | Wang Zhaojun | 4 | A native of Zigui, Nanjun during the Western Han Dynasty | |
##null | null | 5 | Warring States Period | 6 |
4) Full link | id |
idcountry
##1 | xishi | 1 | 元国人 | |
---|---|---|---|---|
Yang Yuhuan | 2 | People from Yongle, Puzhou, Tang Dynasty | 2 | |
Diao Chan | 3 | Late Eastern Han Dynasty Shanxi Xinzhou people | 3 | |
王赵君 | 4 | Western Han Dynasty A native of Zigui, Nanjun | 4 | |
Zhao Feiyan | null | null | null | |
null | 5 | Warring States Period | 6 | |
Note: This syntax is not applicable in MySql | Okay, I have finished the introduction. I wonder if it helps you? | If there is anything wrong, please point it out~For more related knowledge, please pay attention to the | sql
The above is the detailed content of Understand inner joins, left outer joins, and right outer joins of sql statements. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses using SQL for GDPR and CCPA compliance, focusing on data anonymization, access requests, and automatic deletion of outdated data.(159 characters)

The article discusses securing SQL databases against vulnerabilities like SQL injection, emphasizing prepared statements, input validation, and regular updates.

Article discusses implementing data partitioning in SQL for better performance and scalability, detailing methods, best practices, and monitoring tools.

The article discusses using SQL for data warehousing and business intelligence, focusing on ETL processes, data modeling, and query optimization. It also covers BI report creation and tool integration.

The DATETIME data type is used to store high-precision date and time information, ranging from 0001-01-01 00:00:00 to 9999-12-31 23:59:59.99999999, and the syntax is DATETIME(precision), where precision specifies the accuracy after the decimal point (0-7), and the default is 3. It supports sorting, calculation, and time zone conversion functions, but needs to be aware of potential issues when converting precision, range and time zones.

SQL IF statements are used to conditionally execute SQL statements, with the syntax as: IF (condition) THEN {statement} ELSE {statement} END IF;. The condition can be any valid SQL expression, and if the condition is true, execute the THEN clause; if the condition is false, execute the ELSE clause. IF statements can be nested, allowing for more complex conditional checks.

How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

Article discusses use and benefits of stored procedures and functions in SQL, including performance optimization and differences between them.
