• 学习下mysql175. 组合两个表


    mysql还是在学生时代接触的,好久没用过了

    表: Person

    +-------------+---------+
    | 列名 | 类型 |
    +-------------+---------+
    | PersonId | int |
    | FirstName | varchar |
    | LastName | varchar |
    +-------------+---------+
    personId 是该表的主键列。
    该表包含一些人的 ID 和他们的姓和名的信息。
     

    表: Address

    +-------------+---------+
    | 列名 | 类型 |
    +-------------+---------+
    | AddressId | int |
    | PersonId | int |
    | City | varchar |
    | State | varchar |
    +-------------+---------+
    addressId 是该表的主键列。
    该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
     

    编写一个SQL查询来报告 Person 表中每个人的姓、名、城市和州。如果 personId 的地址不在 Address 表中,则报告为空  null 。

    以 任意顺序 返回结果表。

    查询结果格式如下所示。

    示例 1:

    输入:
    Person表:
    +----------+----------+-----------+
    | personId | lastName | firstName |
    +----------+----------+-----------+
    | 1 | Wang | Allen |
    | 2 | Alice | Bob |
    +----------+----------+-----------+
    Address表:
    +-----------+----------+---------------+------------+
    | addressId | personId | city | state |
    +-----------+----------+---------------+------------+
    | 1 | 2 | New York City | New York |
    | 2 | 3 | Leetcode | California |
    +-----------+----------+---------------+------------+
    输出:
    +-----------+----------+---------------+----------+
    | firstName | lastName | city | state |
    +-----------+----------+---------------+----------+
    | Allen | Wang | Null | Null |
    | Bob | Alice | New York City | New York |
    +-----------+----------+---------------+----------+
    解释:
    地址表中没有 personId = 1 的地址,所以它们的城市和州返回 null。
    addressId = 1 包含了 personId = 2 的地址信息。

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/combine-two-tables
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    # Write your MySQL query statement below
    select firstName, lastName, city, State
    from Person left join Address
    on Person.PersonId = Address.PersonId
  • 相关阅读:
    shell
    Python全栈开发:django网络框架(二)
    Python全栈开发:django网络框架(一)
    动态规划问题以及诸多实例分析
    python实现并查集
    使用命令行编译和运行 c、Java和python程序
    整数除法操作的取整问题
    使用TensorFlow低级别的API进行编程
    使用TensorFlow高级别的API进行编程
    TensorFlow安装和HelloWorld
  • 原文地址:https://www.cnblogs.com/caoke/p/16750216.html
Copyright © 2020-2023  润新知