Home Database Mysql Tutorial MySQL左连接、右连接和内连接详解

MySQL左连接、右连接和内连接详解

Jun 07, 2016 pm 04:09 PM
mysql Detailed explanation connect

以MySql为例。在MySQL数据库中建立两张数据表,并分别插入一些数据。 示例脚本如下: drop table table1; CREATE TABLE `andrew`.`table1` ( ` name ` VARCHAR (32) NOT NULL , `city` VARCHAR (32) NOT NULL ) ENGINE=MyISAM; insert into TABLE1( name ,ci

以MySql为例。在MySQL数据库中建立两张数据表,并分别插入一些数据。

示例脚本如下:

<ol class="dp-sql">
<li class="alt"><span><span> </span><span class="keyword">drop</span><span> </span><span class="keyword">table</span><span> table1;   </span></span></li>
<li>
<span></span><span class="keyword">CREATE</span><span> </span><span class="keyword">TABLE</span><span> `andrew`.`table1`   </span>
</li>
<li class="alt"><span>(   </span></li>
<li>
<span>`</span><span class="keyword">name</span><span>` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span>,   </span>
</li>
<li class="alt">
<span>`city` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span>   </span>
</li>
<li><span>)   </span></li>
<li class="alt"><span>ENGINE = MyISAM;   </span></li>
<li>
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person A'</span><span>, </span><span class="string">'BJ'</span><span>);   </span>
</li>
<li class="alt">
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person B'</span><span>, </span><span class="string">'BJ'</span><span>);   </span>
</li>
<li>
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person C'</span><span>, </span><span class="string">'SH'</span><span>);   </span>
</li>
<li class="alt">
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person D'</span><span>, </span><span class="string">'SZ'</span><span>);   </span>
</li>
<li>
<span></span><span class="keyword">commit</span><span>;   </span>
</li>
<li class="alt">
<span></span><span class="keyword">drop</span><span> </span><span class="keyword">table</span><span> table2;   </span>
</li>
<li>
<span></span><span class="keyword">CREATE</span><span> </span><span class="keyword">TABLE</span><span> `andrew`.`table2`   </span>
</li>
<li class="alt"><span>(   </span></li>
<li>
<span>`</span><span class="keyword">name</span><span>` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span>,   </span>
</li>
<li class="alt">
<span>`city` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span>   </span>
</li>
<li><span>)   </span></li>
<li class="alt"><span>ENGINE = MyISAM;   </span></li>
<li>
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person W'</span><span>, </span><span class="string">'BJ'</span><span>);   </span>
</li>
<li class="alt">
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person X'</span><span>, </span><span class="string">'SH'</span><span>);   </span>
</li>
<li>
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person Y'</span><span>, </span><span class="string">'SH'</span><span>);   </span>
</li>
<li class="alt">
<span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person Z'</span><span>, </span><span class="string">'NJ'</span><span>);   </span>
</li>
<li>
<span></span><span class="keyword">commit</span><span>; </span>
</li>
</ol>
Copy after login

1. MySQL外连接 – 左连接结果

table1居左,故谓之左连接。这种情况下,以table1为主,即table1中的所有记录均会被列出。有一下三种情况:

a. 对于table1中的每一条记录对应的城市如果在table2中也恰好存在而且刚好只有一条,那么就会在

返回的结果中形成一条新的记录。如上面Person A和Person B对应的情况。

b. 对于table1中的每一条记录对应的城市如果在table2中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的Person C对应的情况。

c. 对于table1中的每一条记录对应的城市如果在table2中不存在,那么就会在返回的结果中形成一条

条新的记录,且该记录的右边全部NULL。如上面的Person D对应的情况。

不符合上面三条规则的记录不会被列出。

2. MySQL外连接 – 右连接结果

table2居右,故谓之右连接。这种情况下,以table2为主,即table2中的所有记录均会被列出。有一下三种情况:

a. 对于table2中的每一条记录对应的城市如果在table1中也恰好存在而且刚好只有一条,那么就会在

返回的结果中形成一条新的记录。如上面Person X和Person Y对应的情况。

b. 对于table2中的每一条记录对应的城市如果在table1中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的Person W对应的情况。

c. 对于table2中的每一条记录对应的城市如果在table1中不存在,那么就会在返回的结果中形成一条

条新的记录,且该记录的左边全部NULL。如上面的Person Z对应的情况。

不符合上面三条规则的记录不会被列出。

3. MySQL内连接

MySQL内连接的数据记录中,不会存在字段为NULL的情况。可以简单地认为,内链接的结果就是在左连接或者右连接的结果中剔除存在字段为NULL的记录后所得到的结果。甚至可以认为,如果两个表中仅分别剩下内连接运算后所得的数据记录,如table1中只有Person A、Person B和Person C,table2中只有Person W、Person X和Person Y,那么这两个表的之间的左连接和右连接的返回的结果是一样的。

注意:select * from table1 a inner join table2 b on a.city = b.city 和select * from table1 a join table2 b on a.city = b.city 的效果是一样的,即如果join的左边没有诸如left、right或者inner这样的关键字时,缺省的是内连接。另外,MySQL不支持full join。


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles