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通过左外连接查询。rrree
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!

OLTPandOLAParebothessentialforbigdata:OLTPhandlesreal-timetransactions,whileOLAPanalyzeslargedatasets.1)OLTPrequiresscalingwithtechnologieslikeNoSQLforbigdata,facingchallengesinconsistencyandsharding.2)OLAPusesHadoopandSparktoprocessbigdata,withsetup

PatternmatchinginSQLusestheLIKEoperatorandregularexpressionstosearchfortextpatterns.Itenablesflexibledataqueryingwithwildcardslike%and_,andregexforcomplexmatches.It'sversatilebutrequirescarefulusetoavoidperformanceissuesandoveruse.

Learning SQL requires mastering basic knowledge, core queries, complex JOIN operations and performance optimization. 1. Understand basic concepts such as tables, rows, and columns and different SQL dialects. 2. Proficient in using SELECT statements for querying. 3. Master the JOIN operation to obtain data from multiple tables. 4. Optimize query performance, avoid common errors, and use index and EXPLAIN commands.

The core concepts of SQL include CRUD operations, query optimization and performance improvement. 1) SQL is used to manage and operate relational databases and supports CRUD operations. 2) Query optimization involves the parsing, optimization and execution stages. 3) Performance improvement can be achieved through the use of indexes, avoiding SELECT*, selecting the appropriate JOIN type and pagination query.

Best practices to prevent SQL injection include: 1) using parameterized queries, 2) input validation, 3) minimum permission principle, and 4) using ORM framework. Through these methods, the database can be effectively protected from SQL injection and other security threats.

MySQL is popular because of its excellent performance and ease of use and maintenance. 1. Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2. Insert and query data: operate data through INSERTINTO and SELECT statements. 3. Optimize query: Use indexes and EXPLAIN statements to improve performance.

The difference and connection between SQL and MySQL are as follows: 1.SQL is a standard language used to manage relational databases, and MySQL is a database management system based on SQL. 2.SQL provides basic CRUD operations, and MySQL adds stored procedures, triggers and other functions on this basis. 3. SQL syntax standardization, MySQL has been improved in some places, such as LIMIT used to limit the number of returned rows. 4. In the usage example, the query syntax of SQL and MySQL is slightly different, and the JOIN and GROUPBY of MySQL are more intuitive. 5. Common errors include syntax errors and performance issues. MySQL's EXPLAIN command can be used for debugging and optimizing queries.

SQLiseasytolearnforbeginnersduetoitsstraightforwardsyntaxandbasicoperations,butmasteringitinvolvescomplexconcepts.1)StartwithsimplequerieslikeSELECT,INSERT,UPDATE,DELETE.2)PracticeregularlyusingplatformslikeLeetCodeorSQLFiddle.3)Understanddatabasedes


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
