【LeetCode数据库】——组合两个表(175)

一、题目

表1: Person

+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+

PersonId 是上表主键

表2: Address

+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+

AddressId 是上表主键

编写一个 SQL 查询,满足条件:无论 Person是否有地址信息,都需要基于上述两表提供 Person的以下信息:

FirstName, LastName, City, State

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combine-two-tables

二、解答

1. 创建数据库

mysql> create database db4_leetcode charset=utf8;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
......
| db4_leetcode       |
......
+--------------------+
10 rows in set (0.02 sec)

mysql> use db4_leetcode;
Database changed
mysql> show tables;
Empty set (0.00 sec)

2. 创建数据表

mysql> create table Person(PersonId int, FirstName varchar(255), LastName varchar(255));
Query OK, 0 rows affected (0.01 sec)

mysql> desc Person;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| PersonId  | int(11)      | YES  |     | NULL    |       |
| FirstName | varchar(255) | YES  |     | NULL    |       |
| LastName  | varchar(255) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> Create table Address (AddressId int, PersonId int, City varchar(255), State varchar(255));
Query OK, 0 rows affected (0.01 sec)

mysql> desc Address;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| AddressId | int(11)      | YES  |     | NULL    |       |
| PersonId  | int(11)      | YES  |     | NULL    |       |
| City      | varchar(255) | YES  |     | NULL    |       |
| State     | varchar(255) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3. 插入示例数据

mysql> Truncate table Person;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into Person (PersonId, LastName, FirstName) values ('1', 'Wang', 'Allen');
Query OK, 1 row affected (0.00 sec)

mysql> select * from Person;
+----------+-----------+----------+
| PersonId | FirstName | LastName |
+----------+-----------+----------+
|        1 | Allen     | Wang     |
+----------+-----------+----------+
1 row in set (0.00 sec)

mysql> truncate table Address;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into Address (AddressId, PersonId, City, State) values ('1', '2', 'New York City', 'New York');
Query OK, 1 row affected (0.00 sec)

mysql> select * from Address;
+-----------+----------+---------------+----------+
| AddressId | PersonId | City          | State    |
+-----------+----------+---------------+----------+
|         1 |        2 | New York City | New York |
+-----------+----------+---------------+----------+
1 row in set (0.00 sec)

4. 查询数据库

mysql> select FirstName, LastName, City, State
    -> from Person left join Address
    -> on Person.PersonId = Address.PersonId
    -> ;
+-----------+----------+------+-------+
| FirstName | LastName | City | State |
+-----------+----------+------+-------+
| Allen     | Wang     | NULL | NULL  |
+-----------+----------+------+-------+
1 row in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/weixin_37780776/article/details/107702610