leetcode 175. Combine Two Tables
2016-02-17 17:18
459 查看
传送门
Question
Total Accepted: 16436 Total Submissions: 48308 Difficulty: Easy
Table:
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:
Subscribe to see which companies asked this question
SQL Right Join
注释:在某些数据库中, LEFT JOIN 称为 LEFT OUTER JOIN。
"Orders" 表:
您可以使用下面的 SELECT 语句:
结果集:
LEFT JOIN 关键字会从左表 (Persons) 那里返回所有的行,即使在右表 (Orders) 中没有匹配的行。
175. Combine Two Tables
My SubmissionsQuestion
Total Accepted: 16436 Total Submissions: 48308 Difficulty: Easy
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
Subscribe to see which companies asked this question
# Write your MySQL query statement below select FirstName, LastName, City, State from Person left join Address on Person.PersonId = Address.PersonId
LEFT JOIN
SQL LEFT JOIN 关键字
SQL Inner JoinSQL Right Join
SQL LEFT JOIN 关键字
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。LEFT JOIN 关键字语法
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
注释:在某些数据库中, LEFT JOIN 称为 LEFT OUTER JOIN。
原始的表 (用在例子中的):
"Persons" 表:Id_P | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | Oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
Id_O | OrderNo | Id_P |
---|---|---|
1 | 77895 | 3 |
2 | 44678 | 3 |
3 | 22456 | 1 |
4 | 24562 | 1 |
5 | 34764 | 65 |
左连接(LEFT JOIN)实例
现在,我们希望列出所有的人,以及他们的定购 - 如果有的话。您可以使用下面的 SELECT 语句:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName
结果集:
LastName | FirstName | OrderNo |
---|---|---|
Adams | John | 22456 |
Adams | John | 24562 |
Carter | Thomas | 77895 |
Carter | Thomas | 44678 |
Bush | George |
相关文章推荐
- 子div在父div中居中。
- centos(linux)普通用户设置sudo权限
- 程序崩溃时拦截并做出友好的提示给用户
- CentOS 7.1 上安装Cloudera Manager 管理的Hadoop
- JMeter学习(一)工具简单介绍
- 使用Concurrency Visualizer优化性能
- PHP里10个鲜为人知但却非常有用的函数
- HTML5新标签
- lua中的require机制
- 请时刻为自己端正态度
- HUST 1010(KMP应用:最短循环节点)
- hdu 1421 搬寝室,dp
- 最新获取屏幕宽高
- LeetCode 165. Compare Version Numbers 解题报告
- 【转】Nginx搭建反向代理服务器过程详解
- PHP获取时间排除周六、周日的两个方法
- Java程序员常用工具类库 - 目录(转)
- 许朝军
- Relative 定位与Absolute 定位实例
- Lastpass——密码管理工具