leetcode MYSQL数据库题目175

175. Combine Two Tables

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:

 FirstName, LastName, City, State  

题目意思是:编写一个SQL查询语句,该语句为Person表中的每个人提供以上信息,而不管这些人是否有一个地址:
答案是:
select
p.FirstName, p.LastName, a.City, a.State
from
Person p left join Address a on p.PersonId = a.PersonId ;


2、知识点总结

  • 多表left join是会生成一张临时表,并返回给用户
  • A left join B ON 条件表达式
  • ON 子句和 WHERE 子句有什么不同?

ON 条件(“A left join B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行。如果 B 表中没有任何一行数据匹配ON 的条件,将会额外生成一行所有列为 NULL 的数据。
而在匹配阶段 WHERE 子句的条件都不会被使用。仅在匹配阶段完成以后,WHERE子句条件才会被使用。它将从匹配阶段产生的数据中检索过滤。

参考链接:
mysql left join中where和on条件的区别
关于 MySQL LEFT JOIN 你可能需要了解的三点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值