175. Combine Two Tables--left join
来源:互联网 发布:linux ioremap 编辑:程序博客网 时间:2024/06/14 21:13
题目:
Table: Person
+-------------+---------+| Column Name | Type |+-------------+---------+| PersonId | int || FirstName | varchar || LastName | varchar |+-------------+---------+PersonId is the primary key column for this table.
Table: Address
+-------------+---------+| Column Name | Type |+-------------+---------+| AddressId | int || PersonId | int || City | varchar || State | varchar |+-------------+---------+AddressId is the primary key column for this table.
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
FirstName, LastName, City, State
我的答案
select FirstName, LastName, City, Statefrom Person,Addresswhere Person.PersonId=Address.PersonId
答案错误,因为题目中有提到regardless if there is an address for each of those people
没有地址的信息也要显示出来 因此不能使用inner join和where 要使用left join
sql语句中left join、inner join中的on与where的区别
转自http://www.cnblogs.com/VerySky/articles/2180076.html
1 .WHERE子句中使用的连接语句,在数据库语言中,被称为隐性连接。INNER JOIN……ON子句产生的连接称为显性连接。(其他JOIN参数也是显性连接)WHERE 和INNER JOIN产生的连接关系,没有本质区别,结果也一样。但是!隐性连接随着数据库语言的规范和发展,已经逐渐被淘汰,比较新的数据库语言基本上已经抛弃了隐性连接,全部采用显性连接了。
2 .无论怎么连接,都可以用join子句,但是连接同一个表的时候,注意要定义别名,否则产生错误!
a>inner join:理解为“有效连接”,两张表中都有的数据才会显示
left join:理解为“有左显示”,比如on a.field=b.field,则显示a表中存在的全部数据及a\\b中都有的数据,A中有、B没有的数据以null显示
b>right join:理解为“有右显示”,比如on a.field=b.field,则显示B表中存在的全部数据及a\\b中都有的数据,B中有、A没有的数据以null显示
c>full join:理解为“全连接”,两张表中所有数据都显示,实际就是inner +(left-inner)+(right-inner)
3 .join可以分主次表外联接有三种类型:完全外联,左联,右联.
完全外联包含两张表的所有记录.
左联是以左边的表为主,右边的为辅,右联则相反
table a(id, type):
id type
----------------------------------
1 1
2 1
3 2
table b(id, class):
id class
---------------------------------
1 1
2 2
sql语句1:select a.*, b.* from a left join b on a.id = b.id and a.type = 1;
sql语句2:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;
sql语句3:select a.*, b.* from a left join b on a.id = b.id and b.class = 1;
sql语句1的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
sql语句2的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
sql语句3的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1
3 2
由sql语句1可见,left join 中左表的全部记录将全部被查询显示,on 后面的条件对它不起作用,除非再后面再加上where来进行筛选,这就是sql语句2了;由sql语句3可见,on后面的条件中,右表的限制条件将会起作用。
**********************************************************************************
sql语句4:select a.*, b.* from a inner join b on a.id = b.id and a.type = 1;
sql语句5:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql语句6:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql语句7:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;
这四条语句的执行结果一样,如下:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
由此可见,inner join 中on后面的限制条件将全部起作用,这与where的执行结果是一样的。另外,where语句与inner join确实能得到相同的结果,只是效率不同(这个我没有测试过,不过我相信这个结论)。
但是sql语句6是否比sql语句7的效率要低一些,我没有足够的数据量来测试,不过我也相信是如此的。
- leetcode 175. Combine Two Tables left join
- 175. Combine Two Tables--left join
- leetcode175---Combine Two Tables(left join的使用)
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- 175. Combine Two Tables
- LeetCode : 175. Combine Two Tables
- leetcode 175. Combine Two Tables
- leetcode.175. Combine Two Tables
- leetcode 175. Combine Two Tables
- Leetcode 175. Combine Two Tables
- Androidstudio 2.2.3升级到2.3 后出现Refreshing gradle project问题
- oracle安装过程中远程桌面断开连接
- 微信小程序里面let,var,const的了解
- 三.Python爬虫Xpath语法与lxml库的用法
- COJ 2047 白旭东の几何之路
- 175. Combine Two Tables--left join
- (二)设置PDF纸张并在指定位置添加文字
- mysql 创建数据库字段类型
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- 背包问题-LintCode
- Android butterknife view注入框架
- 正则表达式"\\s+"
- 24天学会设计模式------责任链模式