[CareerCup] 15.4 Types of Join 各种交
15.4 What are the different types of joins? Please explain how they differ and why certain types are better in certain situations.
Join是用来联合两个表的,每个表至少需要有一列是相同的,不同的Join类型会返回不同的结果。我们来看一个例子,有两个表,普通饮料和无卡饮料如下:
-- TABLE RegularBeverages
+-----------+-----------+
| Name | Code |
+-----------+-----------+
| Budweiser | BUDWEISER |
| Coca-Cola | COCACOLA |
| Pepsi | PEPSI |
+-----------+-----------+
-- TABLE CalorieFreeBeverages
+----------------+----------+
| Name | Code |
+----------------+----------+
| Diet Coca-Cola | COCACOLA |
| Fresca | FRESCA |
| Diet Pepsi | PEPSI |
| Pepsi Light | PEPSI |
| Purfied water | Water |
+----------------+----------+
如果我们想联合这两个表,有很多种Join可以使用:
内交Inner Join:只会显示相同列匹配的项:
SELECT * FROM RegularBeverages
INNER JOIN CalorieFreeBeverages
ON RegularBeverages.Code = CalorieFreeBeverages.Code; +-----------+----------+----------------+----------+
| Name | Code | Name | Code |
+-----------+----------+----------------+----------+
| Coca-Cola | COCACOLA | Diet Coca-Cola | COCACOLA |
| Pepsi | PEPSI | Diet Pepsi | PEPSI |
| Pepsi | PEPSI | Pepsi Light | PEPSI |
+-----------+----------+----------------+----------+
外交Outer Join:外交会包含内交的结果,同时也会包含一些没有匹配到的结果,外交有如下几种:
- 左交Left Outer Join (Left Join),会包含左表的所有结果,如果没有匹配,右边就是NULL:
SELECT * FROM RegularBeverages
LEFT JOIN CalorieFreeBeverages
ON RegularBeverages.Code = CalorieFreeBeverages.Code; +-----------+-----------+----------------+----------+
| Name | Code | Name | Code |
+-----------+-----------+----------------+----------+
| Coca-Cola | COCACOLA | Diet Coca-Cola | COCACOLA |
| Pepsi | PEPSI | Diet Pepsi | PEPSI |
| Pepsi | PEPSI | Pepsi Light | PEPSI |
| Budweiser | BUDWEISER | NULL | NULL |
+-----------+-----------+----------------+----------+
- 右交Right Outer Join (Right Join),返回右表的所有项,如果没有匹配,左边就是NULL:
SELECT * FROM RegularBeverages
RIGHT JOIN CalorieFreeBeverages
ON RegularBeverages.Code = CalorieFreeBeverages.Code; +-----------+----------+----------------+----------+
| Name | Code | Name | Code |
+-----------+----------+----------------+----------+
| Coca-Cola | COCACOLA | Diet Coca-Cola | COCACOLA |
| Pepsi | PEPSI | Diet Pepsi | PEPSI |
| Pepsi | PEPSI | Pepsi Light | PEPSI |
| NULL | NULL | Fresca | FRESCA |
| NULL | NULL | Purfied water | Water |
+-----------+----------+----------------+----------+
- 全交Full Outer Join,联合左交和右交的结果,不论有没有匹配都把结果显示出来,由于MySQL中没有这个命令,所以我们用Union把左右交的结果并起来:
SELECT * FROM RegularBeverages
LEFT JOIN CalorieFreeBeverages
ON RegularBeverages.Code = CalorieFreeBeverages.Code
UNION
SELECT * FROM RegularBeverages
RIGHT JOIN CalorieFreeBeverages
ON RegularBeverages.Code = CalorieFreeBeverages.Code; +-----------+-----------+----------------+----------+
| Name | Code | Name | Code |
+-----------+-----------+----------------+----------+
| Coca-Cola | COCACOLA | Diet Coca-Cola | COCACOLA |
| Pepsi | PEPSI | Diet Pepsi | PEPSI |
| Pepsi | PEPSI | Pepsi Light | PEPSI |
| Budweiser | BUDWEISER | NULL | NULL |
| NULL | NULL | Fresca | FRESCA |
| NULL | NULL | Purfied water | Water |
+-----------+-----------+----------------+----------+
关于此内容还可以参见我之前的一篇博客SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结。
[CareerCup] 15.4 Types of Join 各种交的更多相关文章
- [CareerCup] 15.7 Student Grade 学生成绩
15.7 Imagine a simple database storing information for students' grades. Design what this database m ...
- [CareerCup] 15.5 Denormalization 逆规范化
15.5 What is denormalization? Explain the pros and cons. 逆规范化Denormalization是一种通过添加冗余数据的数据库优化技术,可以帮助 ...
- [CareerCup] 15.2 Renting Apartment II 租房之二
Write a SQL query to get a list of all buildings and the number of open requests (Requests in which ...
- [CareerCup] 15.1 Renting Apartment 租房
Write a SQL query to get a list of tenants who are renting more than one apartment. -- TABLE Apartme ...
- [CareerCup] 15.6 Entity Relationship Diagram 实体关系图
15.6 Draw an entity-relationship diagram for a database with companies, people, and professionals (p ...
- [CareerCup] 15.3 Renting Apartment III 租房之三
Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...
- 15 Linux Split and Join Command Examples to Manage Large Files--reference
by HIMANSHU ARORA on OCTOBER 16, 2012 http://www.thegeekstuff.com/2012/10/15-linux-split-and-join-co ...
- CareerCup All in One 题目汇总
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结
在SQL语言中,存在着各种Join,有Left Join, Right Join, Inner Join, and Natural Join等,对于初学者来说肯定一头雾水,都是神马跟神马啊,它们之间到 ...
随机推荐
- RAC的QA
RAC: Frequently Asked Questions [ID 220970.1] 修改时间 13-JAN-2011 类型 FAQ 状态 PUBLISHED Appli ...
- 深入分析JavaWeb 技术内幕
1,通过浏览器请求一个资源,会发生以下几种过程 1) http的解析过程,包括对于http请求头和响应头中指令(控制用户浏览器的渲染行为和 服务器的执行逻辑)的解析 2)DNS的解析过程(根据域名获取 ...
- windows phone SDK 8.0 模拟器异常 0x89721800解决办法
删除 APPDATA\LOCAL\Microsoft\Phone Tools\CoreCon\10.0 从新启动即可!
- 咱就入个门之NHibernate映射文件配置(二)
上一篇主要介绍了NHibernate映射文件的基础配置,这篇我们介绍下NHibernate的一对多及多对一配置(文中我直接使用双向关联,即一和多两端都配置,开发中可以只使用一端),同时略带介绍下NHi ...
- xUtils,butterknife...处理findviewbyid
在写android中,经常要出现大量的findviewbyid et_path = (EditText) findViewById(R.id.et_path); tv_info = (TextVi ...
- matlab报错
这可能说明..压根就没有这个函数
- YAML 技术研究
YAML预研文档 YAML概要 YAML是"YAML Ain't a Markup Language"(YAML不是一种置标语言)的递归缩写,早先YAML的意思其实是:" ...
- SpringRMI解析2-RmiServiceExporter逻辑脉络
配置文件是Spring的核心,在配置文件中我们可以看到,定义了两个bean,其中一个是对接口实现类的发布,而另一个则是对RMI服务的发布,使用org.springframework.remoting. ...
- 运用datalist标签实现用户的搜索列表
datalist是一个很强大的HTML5标签,支持一般类似于模糊查询,以前都是需要js来做的.下面是一个datalist配合js的小例子,主要是实现用户是否存在,以及添加过程中是否重复的判断. 首先是 ...
- 如何处理js的跨域问题
在bill.mail.10086.cn域内访问smsrebuild1.mail.10086.cn下的接口出现“阻止跨域源请求” 例如: URL 说明 是否允许通信 http://www.a.com/a ...