Leetcode 175. Combine Two Tables(Easy)
2018-02-01 12:25
323 查看
1.题目
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:
翻译:写一条SQL查询语句,提供在Person表中的每个人的以下信息,不管他们有没有地址。
FirstName, LastName, City, State
2.思路
只要在Person表中即进行查询,使用左连接。条件是两个表中的PersonId一致。3.算法
select FirstName,LastName,City,State from Person left join Address on Person.PersonId = Address.PersonId
4.总结
内连接(连接):两个表中都有对应信息。左连接:左侧的表中包含对应属性即可,右侧表中若无对应属性值用null填充。
右连接:右侧的表中包含对应属性即可,左侧表中若无对应属性值用null填充。
外连接:任意一张表包含要查询信息的属性值即可,没有的用null填充。
举个例子:
表1 person表
person_id | person_name |
1 | Li Ming |
2 | Wang Juan |
person_id | job |
1 | Teacher |
3 | Chief |
①内连接
person_id | person_name | job |
1 | Li Ming | Teacher |
person_id | person_name | job |
1 | Li Ming | Teacher |
2 | Wang Juan | null |
person_id | person_name | job |
1 | Li Ming | Teacher |
3 | null | Chief |
person_id | person_name | job |
1 | Li Ming | Teacher |
2 | Wang Juan | null |
3 | null | Chief |
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/knock.gif)
相关文章推荐
- 【LeetCode】88.Merge Sorted Array(easy)解题报告
- LeetCode-Easy部分中标签为String 383. Ransom Note
- LeetCode-6-ZigZag Conversion(找规律)-Easy
- [Leetcode 168, Easy] Excel Sheet Column Title
- 【LeetCode】(C#)155. Min Stack(Easy)
- Leetcode-easy题目总结-Java实现
- LeetCode解题报告 112. Path Sum [easy]
- 【leetcode】Minimum Depth of Binary Tree (easy)
- [Leetcode 165, Easy] Compare Version Numbers
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (java)
- LeetCode: 继续easy题3
- 用java解 leetcode上easy题2(开始探险)
- LeetCode-19-Remove Nth Node From End of List(链表)-Easy
- leetcode 191 Number of 1 Bits(难易度:Easy)
- 用java解leetcode上easy题3
- 【Leetcode】【Easy】Add Binary
- LeetCode: 继续easy题10
- 【leetcode】【Easy】【104. Maximum Depth of Binary Tree】【tree】
- (easy)LeetCode 205.Isomorphic Strings (*)
- LeetCode 答案(Easy)(101-200)