每日一题-1(组合两表)

题1:

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于下面两表提供 person 的以下信息:FirstName, LastName, City, State
在这里插入图片描述
解题思路:
在解题之前需要了解多表连接的类型:

  1. 左连接(left join),连接结果保留左表的全部数据
  2. 右连接(right join),连接结果保留右表的全部数据
  3. 内连接(inner join),取两表的公共数据

    题目要求无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息。因此person表一定保留了所有person的信息,所以person左连接address,保留了person的信息,不存在的字段显示null。
代码如下:`


SELECT p.FirstName,p.LastName,a.City,a.State
from Person as p
left join Address as a
on p.PersonId=a.PersonId;

猜你喜欢

转载自blog.csdn.net/Txixi/article/details/121153677