嘿,伙计们,我需要一个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。
26条答案
按热度按时间jfewjypa16#
--+
从这个表中,我们只能得到建筑id=1
JOIN room ON person.person_id = room.room_id +---------+bcs8qyzn17#
---+
mtb9vblg18#
--+ | building_id | building_name | +
93ze6v8z19#
---+
55ooxyrt20#
-+ | meeting_id | room_id | meeting_start | meeting_end | +
crcmnpdw21#
--+---------+
8ulbf1ek22#
--+---------+
u1ehiz5o23#
--+ | 1 | Headquarters |<- | 2 | Main Street Building | +
1cosmwyk24#
找到了解决办法。不需要加入。。。
yhived7q25#
---+
u91tlkcl26#
-+