您好,我是小DAI,专注于数据库管理员相关的技术问答,请问有什么可以帮您?

ad_sql__left__join

SQL LEFT JOIN 语法


SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

或:


SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

注释:在某些数据库中,LEFT JOIN 称为 LEFT OUTER JOIN。

  • table1:左表(主表),LEFT JOIN 会保留该表的所有记录。

  • table2:右表(从表),如果没有匹配的数据,用 NULL 填充对应的列。

  • ON table1.column_name=table2.column_name:指定连接条件,通常是两个表的共同字段。

    ![](https://www.runoob.com/wp-content/uploads/2013/09/img_leftjoin.gif)

    特点:

  • 返回左表中的所有记录,即使右表没有匹配的数据。

  • 如果右表没有匹配的记录,结果中该行右表字段为 NULL。

    假设有两个表:Customers 和 Orders。

    Customers 表:

    CustomerID Name
    1 Alice
    2 Bob
    3 Charlie
    4 David

    Orders 表:

    OrderID CustomerID Product
    101 1 Laptop
    102 2 Smartphone

    使用 LEFT JOIN 查询:

    
    SELECT Customers.Name, Orders.Product
    FROM Customers
    LEFT JOIN Orders
    ON Customers.CustomerID = Orders.CustomerID;
    

    查询输出结果:

    Name Product
    Alice Laptop
    Bob Smartphone
    Charlie NULL

    |David NULL

    解释:LEFT JOIN 返回了 Customers 表中的所有记录。对于 Charlie 和 David,由于在 Orders 表中没有匹配的 CustomerID,它们对应的 Product 列为 NULL。

    演示数据库

    在本教程中,我们将使用 RUNOOB 样本数据库。

    下面是选自 "Websites" 表的数据:

    
    +----+--------------+---------------------------+-------+---------+
    +----+--------------+---------------------------+-------+---------+
    <table>
    <tr>
    <th>1</th>
    <th>Google</th>
    <th>https://www.google.cm/</th>
    <th>1</th>
    <th>USA</th>
    </tr>
    <tr>
    <td>3</td>
    <td>菜鸟教程</td>
    <td>http://www.runoob.com/</td>
    <td>4689</td>
    <td>CN</td>
    </tr>
    <tr>
    <td>4</td>
    <td>微博</td>
    <td>http://weibo.com/</td>
    <td>20</td>
    <td>CN</td>
    </tr>
    <tr>
    <td>5</td>
    <td>Facebook</td>
    <td>https://www.facebook.com/</td>
    <td>3</td>
    <td>USA</td>
    </tr>
    <tr>
    <td>7</td>
    <td>stackoverflow</td>
    <td>http://stackoverflow.com/</td>
    <td>0</td>
    <td>IND</td>
    </tr>
    </table>
    
    +----+---------------+---------------------------+-------+---------+
    

    下面是 "access_log" 网站访问记录表的数据:

    
    mysql> SELECT * FROM access_log;
    +-----+---------+-------+------------+
    +-----+---------+-------+------------+
    <table>
    <tr>
    <th>1</th>
    <th>1</th>
    <th>45</th>
    <th>2016-05-10</th>
    </tr>
    <tr>
    <td>3</td>
    <td>1</td>
    <td>230</td>
    <td>2016-05-14</td>
    </tr>
    <tr>
    <td>4</td>
    <td>2</td>
    <td>10</td>
    <td>2016-05-14</td>
    </tr>
    <tr>
    <td>5</td>
    <td>5</td>
    <td>205</td>
    <td>2016-05-14</td>
    </tr>
    <tr>
    <td>6</td>
    <td>4</td>
    <td>13</td>
    <td>2016-05-15</td>
    </tr>
    <tr>
    <td>7</td>
    <td>3</td>
    <td>220</td>
    <td>2016-05-15</td>
    </tr>
    <tr>
    <td>8</td>
    <td>5</td>
    <td>545</td>
    <td>2016-05-16</td>
    </tr>
    <tr>
    <td>9</td>
    <td>3</td>
    <td>201</td>
    <td>2016-05-17</td>
    </tr>
    </table>
    
    +-----+---------+-------+------------+
    9 rows in set (0.00 sec)
    

    SQL LEFT JOIN 实例

    下面的 SQL 语句将返回所有网站及他们的访问量(如果有的话)。

    以下实例中我们把 Websites 作为左表,access_log 作为右表:

    实例

    
    SELECT Websites.name, access_log.count, access_log.date
    FROM Websites
    LEFT JOIN access_log
    ON Websites.id=access_log.site_id
    ORDER BY access_log.count DESC;
    

    执行以上 SQL 输出结果如下:

    ![](https://www.runoob.com/wp-content/uploads/2013/09/left-join1.jpg)

    注释:LEFT JOIN 关键字从左表(Websites)返回所有的行,即使右表(access_log)中没有匹配。

    来源:https://www.runoob.com/sql/sql-join-left.html