【问题标题】:SQL INNER JOIN Multiple Tables and columnsSQL INNER JOIN 多个表和列
【发布时间】:2018-06-18 02:59:53
【问题描述】:

问题:

Construct the SQL statement to find all of the people that have meetings only before Dec. 25, 2016 at noon using INNER JOINs. Display the following columns:
 Person’s first name
 Person’s last name
 Meeting ID
 Meeting start date and time
 Meeting end date and time

表格: 这个数据库有5个表(person, building, room, meeting, person_meeting

+-----------+------------+------------+
| 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 Buidling |
+-------------+----------------------+

+---------+-------------+-------------+----------+
| 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 |
+-----------+------------+

到目前为止我的解决方案:

SELECT first_name,last_name ,building_name,meeting_start,meeting_end 来自 P 人 INNER JOIN B楼 ON P.person_id=PM.person_id INNER JOIN person_meeting PM ON M.room_id

我在完成 SQL 语句时遇到问题,请尽可能提供帮助。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    这可能对你有用。我为连接中的每个表使用了别名,并在您需要的列的选择语句中使用了它们。然后我加入了所需的表格,并在 where 中确定了在 12 月 12 日中午之前带有 meeting_end 的表格。 (我假设,如果您想启动它,只需将其切换到 meeting_start

    select p.first_name,p.last_name,pm.meeting_id,m.meeting_start,m.meeting_end from person p
    inner join person_meeting pm on pm.person_id = p.person_id
    inner join meeting m on m.meeting_id = pm.meeting_id
    where m.meeting_end > '2016-12-25 12:00:00'
    

    【讨论】:

    • 这个帮助?
    猜你喜欢
    • 2011-03-03
    • 2014-01-22
    • 2018-03-07
    • 2011-07-29
    • 2023-03-10
    • 2014-02-24
    • 2019-02-03
    相关资源
    最近更新 更多