sql连接多表mysql

xdnvmnnf  于 2021-06-20  发布在  Mysql
关注(0)|答案(26)|浏览(424)

嘿,伙计们,我需要一个sql语句的帮助。总共有5个表,我需要为select语句连接多个表。以下是表格:

+-----------+------------+------------+
| person_id | first_name | last_name  |
+-----------+------------+------------+
|         1 | Tom        | Hanks      |
|         2 | Anne       | Hathaway   |
|         3 | Tom        | Cruise     |
|         4 | Meryl      | Streep     |
|         5 | Chris      | Pratt      |
|         6 | Halle      | Berry      |
|         7 | Robert     | De Niro    |
|         8 | Julia      | Roberts    |
|         9 | Denzel     | Washington |
|        10 | Melissa    | McCarthy   |
+-----------+------------+------------+

+-------------+----------------------+
| building_id | building_name        |
+-------------+----------------------+
|           1 | Headquarters         |
|           2 | Main Street Building |
+-------------+----------------------+

+---------+-------------+-------------+----------+
| room_id | room_number | building_id | capacity |
+---------+-------------+-------------+----------+
|       1 | 100         |           1 |        5 |
|       2 | 200         |           1 |        4 |
|       3 | 300         |           1 |       10 |
|       4 | 10          |           2 |        4 |
|       5 | 20          |           2 |        4 |
+---------+-------------+-------------+----------+

+------------+---------+---------------------+---------------------+
| meeting_id | room_id | meeting_start       | meeting_end         |
+------------+---------+---------------------+---------------------+
|          1 |       1 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
|          2 |       1 | 2016-12-25 10:00:00 | 2016-12-25 12:00:00 |
|          3 |       1 | 2016-12-25 11:00:00 | 2016-12-25 12:00:00 |
|          4 |       2 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
|          5 |       4 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
|          6 |       5 | 2016-12-25 14:00:00 | 2016-12-25 16:00:00 |
+------------+---------+---------------------+---------------------+

+-----------+------------+
| person_id | meeting_id |
+-----------+------------+
|         1 |          1 |
|        10 |          1 |
|         1 |          2 |
|         2 |          2 |
|         3 |          2 |
|         4 |          2 |
|         5 |          2 |
|         6 |          2 |
|         7 |          2 |
|         8 |          2 |
|         9 |          3 |
|        10 |          3 |
|         1 |          4 |
|         2 |          4 |
|         8 |          5 |
|         9 |          5 |
|         1 |          6 |
|         2 |          6 |
|         3 |          6 |
+-----------+------------+

问题是:构造sql语句来查找tomhanks必须参加的所有会议。显示以下列:人员的名字人员的姓氏建筑名称房间号会议开始日期和时间会议结束日期和时间
我的声明:

SELECT person.first_name, person.last_name, building.building_name,
    ->
    -> room.room_number, meeting.meeting_start, meeting.meeting_end
    ->
    -> FROM person
    ->
    -> JOIN building
    ->
    -> ON person.person_id = building.building_id
    ->
    -> JOIN room
    ->
    -> ON  person.person_id = room.room_id
    ->
    -> JOIN meeting
    ->
    -> ON person.person_id = meeting.meeting_id
    ->
    -> WHERE person_id = 1;

结果:

+------------+-----------+---------------+-------------+---------------------+---------------------+
| first_name | last_name | building_name | room_number | meeting_start       | meeting_end         |
+------------+-----------+---------------+-------------+---------------------+---------------------+
| Tom        | Hanks     | Headquarters  | 100         | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 |
+------------+-----------+---------------+-------------+---------------------+---------------------+

汤姆·汉克斯有多个会议。(准确地说是4个)我如何展示这4个会议?不知道该怎么办。我的陈述只显示了1。

ttisahbt

ttisahbt3#

  • | room_id | room_number | building_id | capacity | +---------+
axkjgtzd

axkjgtzd5#

-+ | 1 | 1 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 | <- | 2 | 1 | 2016-12-25 10:00:00 | 2016-12-25 12:00:00 | | 3 | 1 | 2016-12-25 11:00:00 | 2016-12-25 12:00:00 | | 4 | 2 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 | | 5 | 4 | 2016-12-25 09:00:00 | 2016-12-25 10:00:00 | | 6 | 5 | 2016-12-25 14:00:00 | 2016-12-25 16:00:00 | +

watbbzwu

watbbzwu8#

这个查询应该会给出您想要的(除非有拼写错误:p!) SELECT person.first_name, person.last_name, building.building_name, room.room_number, meeting.meeting_start, meeting.meeting_end FROM person INNER JOIN person_meeting ON person_meeting.person_id = person.person_id INNER JOIN meeting ON meeting.meeting_id = person_meeting.meeting_id INNER JOIN room ON room.room_id = meeting.room_id INNER JOIN building on building.building_id = room.building_id WHERE person.person_id = 1 通过此查询,您将连接已创建关系上的表。一个人通过 meeting.person_id = person.person_id 会议通过 room.room_id = meeting.room_id 一个房间通过 building.building_id = room.room_id 最后过滤这些只显示在哪里 person.person_id = 1 您的查询的问题是您试图通过person\u id将所有内容连接到person `JOIN building ON person.person_id = building.building_id +

k2arahey

k2arahey9#

  • | 1 | 100 | 1 | 5 | <- | 2 | 200 | 1 | 4 | | 3 | 300 | 1 | 10 | | 4 | 10 | 2 | 4 | | 5 | 20 | 2 | 4 | +---------+
2eafrhcq

2eafrhcq12#

-+从这张table上我们可以看出我们只能meeting_id = 1` 主键/外键
person.person\u id是您的pk,meeting.person\u id是您的fk
room.room\u id是您的pk,meeting.room\u id是您的fk
building.building\u id是您的pk,room.building\u id是您的fk
sql小提琴

pbwdgjma

pbwdgjma15#

+从这张table上我们只能 room_id = 1JOIN meeting ON person.person_id = meeting.meeting_id +

相关问题