当前位置:   article > 正文

【LeetCode之MySQL】175. Combine Two Tables_mysql 175. combine two tables

mysql 175. combine two tables

问题:

Table: Person

  1. +-------------+---------+
  2. | Column Name | Type |
  3. +-------------+---------+
  4. | PersonId | int |
  5. | FirstName | varchar |
  6. | LastName | varchar |
  7. +-------------+---------+
  8. PersonId is the primary key column for this table.

Table: Address

  1. +-------------+---------+
  2. | Column Name | Type |
  3. +-------------+---------+
  4. | AddressId | int |
  5. | PersonId | int |
  6. | City | varchar |
  7. | State | varchar |
  8. +-------------+---------+
  9. 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

解决办法:

通过左关联 表A left join 表2 on 相同字段 

左关联(Left Join)就是以左边的表为准,只要左边的表有数据就输出,不管右边的表有没有数据。右关联(Right Join)相反,以右边的表为准。而内关联则是两张表都要有数据才能查询到。外关联则是只要任意一张表有数据就能查询并显示。

不能用where操作 ;如果使用where的话如果右表不存在匹配的行,那么不会返回数据,所以用Left join。

select FirstName, LastName, City, State from Person left join  Address on Person.PersonId=Address.PersonId;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/727057
推荐阅读
相关标签
  

闽ICP备14008679号