mysql|mysql left join on 左表查询

mysql> select * from employee_tbl;

【mysql|mysql left join on 左表查询】+----+--------+---------------------+--------+
| id | name| date| singin |
+----+--------+---------------------+--------+
|1 | 小明| 2016-04-22 15:25:33 |1 |
|2 | 小王| 2016-04-20 15:25:47 |3 |
|3 | 小丽| 2016-04-19 15:26:02 |2 |
|4 | 小王| 2016-04-07 15:26:14 |4 |
|5 | 小明| 2016-04-11 15:26:40 |4 |
|6 | 小明| 2016-04-04 15:26:54 |2 |
+----+--------+---------------------+--------+
6 rows in set (0.00 sec)
mysql> select *from students;
+----+--------+-----+------+--------+
| id | name| sex | age| singin |
+----+--------+-----+------+--------+
|1 | 小明| 男|12 |1 |
|2 | 小丽| 女|15 |0 |
|3 | 王芳| 女|20 |3 |
+----+--------+-----+------+--------+
3 rows in set (0.00 sec)
1、左表查询,以左边的表为准,没有数据的使用NULL 表示
给表取别名:
mysql> selecta.id,a.name,b.singinfrom students aleft joinemployee_tbl bona.name = b.name;
或者不给表取别名:
mysql>select students.id,students.name,employee_tbl.singin from students left jion employee on students.name = employee.name;
+----+--------+--------+
| id | name| singin |
+----+--------+--------+
|1 | 小明|1 |
|2 | 小丽|2 |
|1 | 小明|4 |
|1 | 小明|2 |
|3 | 王芳|NULL |
+----+--------+--------+

    推荐阅读