加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

175. Combine Two Tables

发布时间:2020-12-15 01:15:01 所属栏目:C语言 来源:网络整理
导读:Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table. Table:
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

按以上的数据格式查询,无论地址表有没有数据都要查询出来。

  • 因为Person表中有的数据可能在Address表中没有数据,所以需要使用right join,无论在Address中有没有数据都回查询出来
select 
 p.FirstName,p.LastName,a.City,a.State 
from 
 address a 
right join 
 Person p 
on a.PersonId = p.PersonId

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读