MySQL LEFT JOIN
来源:互联网 发布:mac双系统卸载windows 编辑:程序博客网 时间:2024/06/05 05:18
MySQL LEFT JOIN
现在有两个表: Customers,Orders
它们有如下列:
mysql> describe Orders;
+————+———-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+————+———-+——+—–+———+——-+
| order_num | int(11) | NO | PRI | NULL | |
| order_date | datetime | NO | | NULL | |
| cust_id | char(10) | NO | | NULL | |
+————+———-+——+—–+———+——-+
3 rows in set (0.01 sec)
mysql> describe Customers;
+————–+———–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+————–+———–+——+—–+———+——-+
| cust_id | char(10) | NO | PRI | NULL | |
| cust_name | char(50) | NO | | NULL | |
| cust_address | char(50) | YES | | NULL | |
| cust_city | char(50) | YES | | NULL | |
| cust_state | char(5) | YES | | NULL | |
| cust_zip | char(10) | YES | | NULL | |
| cust_country | char(50) | YES | | NULL | |
| cust_contact | char(50) | YES | | NULL | |
| cust_email | char(255) | YES | | NULL | |
+————–+———–+——+—–+———+——-+
语法
select t1.c1, t1.c2, t2.c1, t2.c2from T1 t1 left join T2 t2 on t1.c1=t2.c1
左外连接包含的结果集
左外连接查询的结果不仅包括 表T1,T2 中相匹配的行,还包括T1(left join左边的表)中没有在T2中与之相匹配的行。
查询Customers 中的顾客的订单
一个顾客可以有0到多个订单,一个订单只属于一个顾客。
select c.cust_id, c.cust_name, o.order_date from Customers c left join Orders o on c.cust_id=o.cust_id;
查询结果:
+————+—————+———————+
| cust_id | cust_name | order_date |
+————+—————+———————+
| 1000000001 | Village Toys | 2012-05-01 00:00:00 |
| 1000000003 | Fun4All | 2012-01-12 00:00:00 |
| 1000000004 | Fun4All | 2012-01-30 00:00:00 |
| 1000000005 | The Toy Store | 2012-02-03 00:00:00 |
| 1000000001 | Village Toys | 2012-02-08 00:00:00 |
| 1000000002 | Kids Place | NULL |
+————+—————+———————+
虽然有的顾客没有订单,但仍然出现在了结果集中
- MySQL Left Join,Right Join
- MySQL Left Join,Right Join
- mysql left join right join
- MYSQl left join 分析
- LEFT JOIN in MySQL
- mysql left join分析
- MySQL:union、left join
- MySQL LEFT JOIN
- 关于MySql Left Join
- mysql left join
- mysql left join写法
- mysql update left join
- mysql update left join
- MySQL left join优化
- mysql left join right join inner join
- mysql left join,right join,inner join
- Mysql-left join,right join,inner join
- mysql left join,right join,inner join
- 安卓 ImageView ACTION_UP&DOWN监听
- [iOS]The app icon set named "AppIcon" did not have any applicable content
- 现在的问题
- Retrofit 2.0 (v2.0.0-beta4)
- B-Queue on Bus Stop
- MySQL LEFT JOIN
- 人物动画的控制
- HDU 5626 Clarke and points
- 初窥Linux 之 我最常用的20条命令
- poj1014 dfs/背包/类筛法求素数
- 【SPOJ-BTTNS】Buttons【高斯消元】【异或方程组】
- __stdcall,__cdecl,__fastcall的区别
- 剑指Offer:包含min函数的栈(JAVA)
- HDU 5627 Clarke and MST