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 |
+----+--------+--------+
推荐阅读
- MySQL|深入浅出MySQL灵魂十连问,你真的有把握吗()
- MySQL|来自大厂面试官的MySQL灵魂十连问,你真的有把握全部回答出来吗()
- MySQL灵魂十连
- 【有码教育】PHP环境(apache|【有码教育】PHP环境(apache,PHP,Mysql)详细配置方法
- MySQL更新锁表超时 Lock wait timeout exceeded
- MySQL更新锁表超时|MySQL更新锁表超时 Lock wait timeout exceeded
- 读书笔记|【读书笔记】 mysql是怎么运行的
- mysql|MySQL索引原理以及SQL优化
- centos/windows服务器,Mysql数据库表结构损坏-已解决
- MySQL数据库|MySQL数据库二(MySQL索引)