MySQL左连接、右连接和内连接详解
以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>
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。

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

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.

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.

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 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.

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.

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.

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

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.
