sql连接多表mysql

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

嘿,伙计们,我需要一个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。

jfewjypa

jfewjypa16#

--+从这个表中,我们只能得到建筑id=1JOIN room ON person.person_id = room.room_id +---------+

mtb9vblg

mtb9vblg18#

--+ | building_id | building_name | +

55ooxyrt

55ooxyrt20#

-+ | meeting_id | room_id | meeting_start | meeting_end | +

u1ehiz5o

u1ehiz5o23#

--+ | 1 | Headquarters |<- | 2 | Main Street Building | +

1cosmwyk

1cosmwyk24#

找到了解决办法。不需要加入。。。

SELECT first_name,last_name ,building_name,room_number ,meeting_start,meeting_end
FROM person P, building B,person_meeting PM,meeting M,room R
WHERE
P.person_id=PM.person_id
AND M.room_id=R.room_id
AND B.building_id=R.building_id
AND PM.meeting_id=M.meeting_id
AND P.first_name='Tom'
AND P.last_name='Hanks';

相关问题