Table of Contents
内连接与左右连接
内连接的匹配方式
左右连接的匹配过程
Home Database Mysql Tutorial mysql左连接,右连接,内连接学习体会

mysql左连接,右连接,内连接学习体会

Jun 07, 2016 pm 03:33 PM
mysql statement connect

声明:本文为本人在分析学习了其他网友的博文后,得出的一些体会。。编写本文的目的,一则,分享自己的学习体会,希望通过此文与其他正在学习数据库的网友互相交流,共同学习;二则,记录下学习中的点滴收获,供以后温习。本人为数据库初学者,有很多观点可

声明:本文为本人在分析学习了其他网友的博文后,得出的一些体会。。编写本文的目的,一则,分享自己的学习体会,希望通过此文与其他正在学习数据库的网友互相交流,共同学习;二则,记录下学习中的点滴收获,供以后温习。本人为数据库初学者,有很多观点可能存在错误,若诸位网友有不同观点,望不吝指教。

首先引用一下这位网友的博文,本人从此博文中受益良多,感谢此位网友的分享。

http://hi.baidu.com/woaidelphi/item/d96be15c3ce2c1cbd2e10c6c

author:华夏之星
QQ:1019822077
email:woai_php@sina.com

内连接: 只连接匹配的行
左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行
右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边表中全部匹配的行
注:此博文中还讲述了其他连接方式,鉴于本人刚开始学习数据库,因此,仅研究较为普遍的3中方式。

内连接与左右连接

在网上看到很多讲述内连接和左右连接的博文,一直认为这是3类连接方式。但看完这篇博文后,认识到,其实这3中连接方式,实际可以分为

两类。一种内连接,一种外连接。而外连接又可分为左连接和右连接。

内连接的匹配方式

------------------------------------------------------------------------------再次引用-----------------------------------------------------------------------

举个例子吧。
表A
id   name  
1    张
2    李
3    王

表B
id   address   A_id
1    北京      1
2    上海      3
3    南京      10

/******************************** inner join  内连接*****************************************/
排他性:A,B表中至少有1个匹配时,才返回行。两表的交集
SQL语句如下:
select A.name,B.address from A 
inner join B
on A.id = B.A_id

查询结果为:
name     address
张     北京
王     上海

------------------------------------------------------------------------------引用结束-----------------------------------------------------------------------

        此sql查询语句可按如下翻译:使用内连接的方式,选择出A中id与B中A_id相等的A表和B表中的行,然后将A表中行的name字段和B表中的address字段组合成行,作为查询结果。

        具体过程可以描述为,对于A表中id为1的行(及表中第一行),与B表中的每一行都进行匹配,找到与B表中A_id字段相同的行(本例中B表的第一行便满足条件)。由于本例中,A表的第一行和B表的第一行便满足SQL语句的查询条件,因此,查询结果集的第一条结果为A表第一行的name(张)和B表第一行的address(北京)。然后,对A表的第二行进行匹配,遍历B表的所有行,没有找到满足与A表id(2)相同的A_id的行,因此。接着为A表的第3行,在B表中寻找匹配的行。遍历B表中所有的行,发现第二行的A_id(3)与A表id(3)匹配,因此,匹配成功,输出结果。自此,A表的所有行的查询了,本次SQL查询结束。结果有两条匹配的结果。

左右连接的匹配过程

--------------------------------------------------------------------------接着引用---------------------------------------------------------------

表A
id   name  
1    张
2    李
3    王

表B
id   address   A_id
1    北京      1
2    上海      3
3    南京      10

/******************************** left join  左连接*****************************************/
包容性:A表包容B表,左连接左表是全的.(left join 或 left outer join )
SQL语句如下:
SELECT A.name, B.address
FROM A
LEFT JOIN B ON A.id = B.A_id 
查询结果为:
name     address
张     北京
李     NULL
王     上海
/******************************** right join  右连接*****************************************/
包容性:B表包容A表,右连接右表是全的.(right join 或 right outer join )
SQL语句如下:
SELECT A.name, B.address
FROM A
RIGHT JOIN B ON A.id = B.A_id 
查询结果为:
name     address
张     北京
王     上海
NULL     南京        

------------------------------------------------------------------------------引用结束-----------------------------------------------------------------------

        左右连接的匹配过程与内连接的匹配过程相同,不同的是,左右连接查询时,SQL语句中,指定的基准表(right join 或left join 后的表)字段

无论是否在另一张表中有与之匹配的行,基准表中的字段均会输出。如上例左连接中的第二行,李(左连接基准表指定的查询字段)和右连接中的

第3行南京(右连接基准表中指定的查询字段)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

What software is better for yi framework? Recommended software for yi framework What software is better for yi framework? Recommended software for yi framework Apr 18, 2025 pm 11:03 PM

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

See all articles