175. Combine Two Tables

前端之家收集整理的这篇文章主要介绍了175. Combine Two Tables前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. Table: Person
  2. +-------------+---------+

  3. | Column Name | Type |

  4. +-------------+---------+

  5. | PersonId | int |

  6. | FirstName | varchar |

  7. | LastName | varchar |

  8. +-------------+---------+

  9. PersonId is the primary key column for this table.

  10. Table: Address

  11. +-------------+---------+

  12. | Column Name | Type |

  13. +-------------+---------+

  14. | AddressId | int |

  15. | PersonId | int |

  16. | City | varchar |

  17. | State | varchar |

  18. +-------------+---------+

  19. AddressId is the primary key column for this table.

  20. 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:

  21. FirstName,LastName,City,State

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

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

猜你在找的程序笔记相关文章