为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL LEFT JOIN语句

标签:
MySQL

Summary: in this tutorial, you will learn about MySQL LEFT JOIN clause and how to apply it to query data from two or more database tables.

Introducing to MySQL LEFT JOIN

The MySQL LEFT JOIN clause allows you to query data from two or more database tables. The LEFT JOIN clause is an optional part of the SELECT statement, which appears after the FROM clause.

Let’s assume that we are going to query data from two tables T1 and T2. The following is the syntax of the LEFT JOIN clause that joins the two tables:

SELECT T1.c1, T1.c2,... T2.c1,T2.c2 FROM T1 LEFT JOIN T2 ON T1.c1 = T2.c1...

When we join the T1 table  to the T2 table using the LEFT JOIN clause, if a row from the left table T1 matches a row from the right table T2 based on the join condition ( T1.c1 = T2.c1), this row is included in the result set. In case the row in the left table does not match the row in the right table, the row in the left table is also selected and combined with a “fake” row from the right table. The fake row contains NULL values for all corresponding columns in the SELECTclause.

In other words, the LEFT JOIN clause allows you to select rows from the both left and right tables that match, plus all rows from the left table ( T1) even there is no match found for them in the right table ( T2).

The following Venn diagram helps you visualize how the MySQL LEFT JOIN clause works. The intersection between two circles are rows that match in both tables, and the remaining part of the left circle are rows in the T1 table that do not have matches in the T2 table. All rows in the left table are included in the result set.

MySQL LEFT JOIN - Venn Diagram

MySQL LEFT JOIN – Venn Diagram

Notice that the returned rows must also match the condition in the WHERE and HAVING clauses if those clauses are available in the query.

MySQL LEFT JOIN Examples

MySQL LEFT JOIN clause – joining  2 tables example

Let’s take a look at the customers and orders tables.

MySQL LEFT JOIN Tables

In the database diagram above:

  • Each order in the orders table must belong to a customer in the customers table.

  • Each customer in the customers table can have zero or more orders in the orders table.

TO find all orders that belong to each customer, you can use the LEFT JOIN clause as follows:

SELECT c.customerNumber,        c.customerName,        orderNumber,         o.status FROM customers c LEFT JOIN orders o ON c.customerNumber = o.customerNumber

MySQL LEFT JOIN - customers and orders

The left table is the customers therefore all customers are included in the result set. However, there are rows in the result set that have customer data but no order data e.g. 168, 169, etc. The order data in these rows are NULL. It means that those customers do not have any order in the orders table.

If you replace the LEFT JOIN clause by the INNER JOIN clause, you only get the customers who have orders in the orders table.

MySQL LEFT JOIN clause to find unmatched rows

The LEFT JOIN clause is very useful when you want to find the rows in the left table that do not match with the rows in the right table. To find the unmatched rows between two tables, you add a WHERE clause to the SELECTstatement to select only rows whose column values in the right table contains the NULL values.

For example, to find all customers who have not ordered any product, you can use the following query:

SELECT c.customerNumber,        c.customerName,        orderNumber,         o.status FROM customers c LEFT JOIN orders o ON c.customerNumber = o.customerNumber WHERE orderNumber IS NULL

MySQL LEFT JOIN -  find unmatched rows

In this tutorial, we have explained the MySQL LEFT JOIN clause and shown you how to apply it to query data from multiple database tables.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/mysql-left-join

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消