博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A2-02-14.DML- MySQL LEFT JOIN
阅读量:4597 次
发布时间:2019-06-09

本文共 4977 字,大约阅读时间需要 16 分钟。

转载自:

 

MySQL LEFT JOIN

 

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.

Introduction 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  statement, which appears after the FROM clause.

Let’s assume that you are going to query data from two tables t1 and t2. The following statement illustrates the syntax of LEFT JOIN clause that joins the two tables:

When you join the t1 table to the t2 table using the LEFT JOIN clause, if a row from the left table t1matches a row from the right table t2 based on the join condition ( t1.c1 = t2.c1 ), this row will be included in the result set.

In case the row in the left table does not match with 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  for all corresponding columns in the SELECT clause.

In other words, the LEFT JOIN clause allows you to select rows from the both left and right tables that are matched, plus all rows from the left table ( t1 ) even with no matching rows found in the right table ( t2 ).

The following Venn diagram helps you visualize how the 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 any matching row in the t2 table. Hence, all rows in the left table are included in the result set.

MySQL LEFT JOIN - Venn DiagramMySQL LEFT JOIN – Venn Diagram

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

MySQL LEFT JOIN examples

Using MySQL LEFT JOIN clause to join two tables

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

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:

MySQL LEFT JOIN example

The left table is 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 these customers do not have any order in the orders table.

Because we used the same column name ( orderNumber) for joining two tables, we can make the query shorter by using the following syntax:

In this statement, the clause

is equivalent to

If you replace the LEFT JOIN clause by the  clause, you get the only customers who have placed at least one order.

Using 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 unmatching rows between two tables, you add a clause to the SELECT statement to query only rows whose column values in the right table contains the NULL values.

For example, to find all customers who have not placed any order, you use the following query:

MySQL LEFT JOIN unmatched rows

Condition in WHERE clause vs. ON clause

See the following example.

In this example, we used the LEFT JOIN clause to query data from the  orders and  orderDetails tables. The query returns an order and its detail, if any, for the order 10123.

MySQL LEFT JOIN - Condition in WHERE clause

However, if you move the condition from the WHERE clause to the ON clause:

It will have a different meaning.

In this case, the query returns all orders but only the order 10123 will have detail associated with it as shown below.

MySQL LEFT JOIN - Condition in ON clause

Notice that for  clause, the condition in the ON clause is equivalent to the condition in the WHERE clause.

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.

转载于:https://www.cnblogs.com/zhuntidaoren/p/9517387.html

你可能感兴趣的文章
python连接zookeeper的日志问题
查看>>
pycharm上传代码到码云错误现象用户密码
查看>>
柔性数组-读《深度探索C++对象模型》有感
查看>>
rmdir 命令(转)
查看>>
生产者消费者
查看>>
Contos 安装Tomcat
查看>>
Python编程Day3—基本运算符、数据类型
查看>>
在Delphi中静态调用DLL 引用外部Dll External Dll 导入Dll
查看>>
How to get AutoCAD Mtext content
查看>>
程序员技术练级攻略
查看>>
Java开发微信公众号
查看>>
【C语言】给一组组数,仅仅有两个数仅仅出现了一次,其它全部数都是成对出现的,找出这两个数。...
查看>>
CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)...
查看>>
18.5.19 自测
查看>>
决策树(Decision Trees)
查看>>
为什么Java字符串是不可变对象?
查看>>
最近挖的坑
查看>>
python-变量
查看>>
MD5-总结
查看>>
Linq to Entity 时间差作为筛选条件产生的问题
查看>>