SQL LEFT JOIN 关键字

SQL LEFT JOIN 关键字从左表 ( table1 )返回所有的行,即使右表 ( table2 ) 中没有匹配。如果右表中没有匹配,则结果为 NULL

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

在某些数据库中,LEFT JOIN 称为 LEFT OUTER JOIN

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

如果不理解,那么就看图解

假设我们有两张表 A 和 B

可以看到每个表的最后一条记录是不匹配的,那么 LEFT JOIN 的结果就是

演示数据

先在 MySQL 数据库运行下面的语句创建测试数据

CREATE DATABASE IF NOT EXISTS twle default character set utf8mb4 collate utf8mb4_unicode_ci;

USE twle;

DROP TABLE IF EXISTS `lession`;
DROP TABLE IF EXISTS `lession_views`;

CREATE TABLE `lession` (
    id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name varchar(32) default '',
    views int(11) NOT NULL default '0',
    created_at DATETIME
);

CREATE TABLE `lession_views` (
    uniq bigint(20) primary key NOT NULL default '0' ,
    lession_id int(11) default '0',
    date_at  int(11) NOT NULL default '0',
    views int(11) NOT NULL default '0'
);

INSERT INTO lession(id,name,views,created_at) VALUES
(1, 'Python 基础教程',981,'2017-04-18 13:52:03'),
(3, 'Ruby 基础教程',199,'2017-05-01 06:16:14'),
(4, 'SQL 基础教程', 300,'2017-05-02 08:13:16');

INSERT INTO lession_views(uniq,lession_id,date_at,views) VALUES
(20170511000001,1,20170511,320),
(20170511000002,2,20170511,22),
(20170511000003,3, 20170511,49),
(20170512000001,1,20170512,220),
(20170512000002,2,20170512,12),
(20170512000003,3,20170512,63),
(20170513000001,1,20170513,441),
(20170513000002,2,20170513,39),
(20170513000003,3,20170513,87);

使用 SELECT * FROM lession; 运行结果如下

mysql> SELECT * FROM lession;
+----+---------------------+-------+---------------------+
| id | name                | views | created_at          |
+----+---------------------+-------+---------------------+
|  1 | Python 基础教程     |   981 | 2017-04-18 13:52:03 |
|  3 | Ruby 基础教程       |   199 | 2017-05-01 06:16:14 |
|  4 | SQL 基础教程        |   300 | 2017-05-02 08:13:16 |
+----+---------------------+-------+---------------------+

使用 SELECT * FROM lession_views; 运行结果如下

mysql> SELECT * FROM lession_views;
+----------------+------------+----------+-------+
| uniq           | lession_id | date_at  | views |
+----------------+------------+----------+-------+
| 20170511000001 |          1 | 20170511 |   320 |
| 20170511000002 |          2 | 20170511 |    22 |
| 20170511000003 |          3 | 20170511 |    49 |
| 20170512000001 |          1 | 20170512 |   220 |
| 20170512000002 |          2 | 20170512 |    12 |
| 20170512000003 |          3 | 20170512 |    63 |
| 20170513000001 |          1 | 20170513 |   441 |
| 20170513000002 |          2 | 20170513 |    39 |
| 20170513000003 |          3 | 20170513 |    87 |
+----------------+------------+----------+-------+

SQL LEFT JOIN 范例

假设我们要查看所有已经存在的课程的每天的访问量,那么可以使用下面的 SQL 语句

SELECT lession.id,lession.name,lession_views.date_at, lession_views.views FROM lession LEFT JOIN lession_views ON lession.id=lession_views.lession_id ORDER BY lession.id DESC;

输出结果如下

mysql> SELECT lession.id,lession.name,lession_views.date_at, lession_views.views FROM lession LEFT JOIN lession_views ON lession.id=lession_views.lession_id ORDER BY lession.id DESC;
+----+---------------------+----------+-------+
| id | name                | date_at  | views |
+----+---------------------+----------+-------+
|  4 | SQL 基础教程        |     NULL |  NULL |
|  3 | Ruby 基础教程       | 20170511 |    49 |
|  3 | Ruby 基础教程       | 20170512 |    63 |
|  3 | Ruby 基础教程       | 20170513 |    87 |
|  1 | Python 基础教程     | 20170511 |   320 |
|  1 | Python 基础教程     | 20170512 |   220 |
|  1 | Python 基础教程     | 20170513 |   441 |
+----+---------------------+----------+-------+

SQL 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2018 简单教程 twle.cn All Rights Reserved.