• 力扣(LeetCode)175. 组合两个表(2022.06.24)


    SQL架构:

    Create table If Not Exists Person (personId int, firstName varchar(255), lastName varchar(255))
    Create table If Not Exists Address (addressId int, personId int, city varchar(255), state varchar(255))
    Truncate table Person
    insert into Person (personId, lastName, firstName) values ('1', 'Wang', 'Allen')
    insert into Person (personId, lastName, firstName) values ('2', 'Alice', 'Bob')
    Truncate table Address
    insert into Address (addressId, personId, city, state) values ('1', '2', 'New York City', 'New York')
    insert into Address (addressId, personId, city, state) values ('2', '3', 'Leetcode', 'California')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    表: 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

    mysql提交内容:

    # Write your MySQL query statement below
    SELECT firstName, lastName, city, state FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId
    
    • 1
    • 2
  • 相关阅读:
    【JAVASE】JDK8新特性
    【React】Table表头纵向展示
    laravel8-rabbitmq消息队列-实时监听跨服务器消息
    如何通过NTC热敏电阻计算温度(一)---理论篇
    前端ES6/7/8/9/10,CSS3,HTML5新特性总结
    .Net中Redis的基本使用
    OpenGL_Learn15(投光物)
    艾美捷Abnova NEUROD6单克隆抗体(M17)说明书
    微信小程序隐私协议弹窗
    MySQL中的字符集与排序规则详解
  • 原文地址:https://blog.csdn.net/ChaoYue_miku/article/details/125465518