首页 > 开发 > 综合 > 正文

175. Combine Two Tables

2024-07-21 02:52:25
字体:
来源:转载
供稿:网友

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

这里要求不论Address信息有没有都要保留Person的信息,这里是left join.

select Person.FirstName, Person.LastName, Address.City, Address.State from Personleft join Addresson Person.PersonId=Address.PersonId;SQL 连接:

1. INNER JOIN:如果表中有至少一个匹配,则返回行;

2. LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行;

3. RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行

来自:点击打开链接


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表