0

已解决运行 UPDATE building SET building_name = "Main Street Building" WHERE building_id = 2; 从那里,任务 3 查询将返回正确的结果。

任务一:

询问:

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

任务二:

询问:

SELECT first_name, last_name, building_name, room.room_id, meeting_start,meeting_end
FROM meeting, person, person_meeting,room, building
WHERE room.room_id=meeting.room_id
AND meeting.meeting_id=person_meeting.meeting_id
AND person.person_id=person_meeting.person_id
AND room.building_id=building.building_id
AND meeting.meeting_id=2;

任务三:

询问:

SELECT first_name, last_name, building_name, room.room_id,meeting.meeting_id, meeting_start, meeting_end
FROM meeting, person, person_meeting,room, building
WHERE room.room_id=meeting.room_id
AND meeting.meeting_id=person_meeting.meeting_id
AND person.person_id=person_meeting.person_id
AND room.building_id=building.building_id
AND building_name='Main Street Building';

任务四:

询问:

SELECT count(person_id) 'Count of meeting attendees', meeting.meeting_id,meeting_start,meeting_end
FROM meeting, person_meeting
WHERE meeting.meeting_id=person_meeting.meeting_id
GROUP BY meeting.meeting_id;

任务五:

询问:

SELECT first_name, last_name, meeting.meeting_id, meeting_start, meeting_end
FROM meeting INNER JOIN person_meeting ON meeting.meeting_id=person_meeting.meeting_id
INNER JOIN person ON person.person_id=person_meeting.person_id
AND meeting_start<'2016-12-25 12.00.00';

任务 1、2、4、5 和 5 都运行良好。如果你们都需要任务提示,请告诉我。

4

1 回答 1

0

注意:我还没有测试这个。

我根据我们最近的互动更改了查询。

这是你的原创

SELECT first_name, last_name, building_name, room.room_id,meeting.meeting_id, meeting_start, meeting_end    
FROM meeting, person, person_meeting,room, building    
WHERE room.room_id=meeting.room_id    
    AND meeting.meeting_id=person_meeting.meeting_id    
    AND person.person_id=person_meeting.person_id    
    AND room.building_id=building.building_id    
    AND building_name='Main Street Building';

这就是我尝试构建它的方式

根据您提供的文档中的第 2 页和第 3 页,https://snhu.brightspace.com/d2l/lor/viewer/viewFile.d2lfile/76437/9523,2/,我将执行以下操作:

首先,我会获取有关建筑物的信息。

SELECT building_id
FROM building
WHERE building_name like  '%Main Street Building%';

注意:我假设建筑物名称是正确的。但是,以防万一,我在名称前后使用通配符 %

接下来,我将获取有关房间的信息:

SELECT b.building_name, r.room_id
FROM room r
  LEFT JOIN building b
    ON r.building_id = b.building_id
WHERE b.building_name like  '%Main Street Building%';

之后,我将获得有关会议的信息:

SELECT b.building_name, r.room_id, m.meeting_id, m.meeting_start, m.meeting_end
FROM room r
  LEFT JOIN building b
    ON r.building_id = b.building_id
  LEFT JOIN meeting m
    ON r.room_id = m.room_id
WHERE b.building_name like  '%Main Street Building%';

最后,我将获取将参加该会议的人员的 ID 信息并显示他们的姓名:

SELECT b.building_name, r.room_id, m.meeting_id, m.meeting_start, m.meeting_end, p.first_name, p.last_name
FROM room r
  LEFT JOIN building b
    ON r.building_id = b.building_id
  LEFT JOIN meeting m
    ON r.room_id = m.room_id
  LEFT JOIN persom_meeting pm
    ON m.meeting_id = pm.meeting_id
  LEFT JOIN person p
    ON pm.person_id = p.person_id
WHERE b.building_name like  '%Main Street Building%';

如果有任何机会,这些查询不起作用,我建议您确保表之间存在关系。

这意味着表之间应该有一个 building_id 匹配、一个 room_id 匹配、一个 meeting_id 匹配和一个 person_id 匹配。另外,我会检查建筑物名称是否拼写正确,因为它区分大小写。

我使用 LEFT JOIN 的原因是我可以显示以前表(所有行)的所有信息以及 ID 与外键 ID 匹配的记录。如此处所述:http ://www.acarlstein.com/?p=4168

在此处输入图像描述

此外,在http://www.acarlstein.com/?p=4194中,我展示了两个左(外)连接是如何工作的。

在此处输入图像描述

但是,在您的情况下,它更像是一个正在构建的链,因此它与图表不匹配,但您也许可以理解我的意思。

于 2019-04-08T18:07:21.127 回答