我遇到了一个问题,主要是安排会议室的时间,我正在设法找到最好的方法,以便在SQL中明智地存储和检索这些数据。
例如:
possible_features = beer_fridge, vga_projector, hdmi_projector, doughnut_machine, conference_phone
meeting_rooms = {
room_a: (beer_fridge,hdmi_projector),
room_b: (beer_fridge,vga_projector,conference_phone),
room_c: (vga_projector,hdmi_projector,conference_phone,doughnut_machine),
}
meetings = {
morning: (hdmi_projector),
devs: (beer_fridge,hdmi_projector),
evening: (conference_phone,vga_projector),
}我需要问的问题有:
在数据库中表示这些数据的合理方法是什么(预先不知道固定设备的列表)并对其进行查询?
这个图案叫什么名字?我肯定我不是第一个有这个问题的人。
(如果答案是特定于DBMS的,我们将使用MySQL)
发布于 2014-11-20 16:53:24
我看到五张桌子:
这没什么特别的。这是一个规范化的数据库,仅此而已。
获取会议房间的查询是:获取所有不存在该会议室中不存在的会议功能的房间。
select r.*
from room r
where not exists
(
select *
from meating_feature mf
where mf.id_meeting = 1
and not exists
(
select *
from room_feature rf
where rf.id_feature = mf.id_feature and rf.id_room = r.id_room
)
);然而,大多数dbms不支持这种深度。r.id_room在第二个不存在子查询中不再为人所知。因此,我们必须以稍微不同的方式编写查询:
select r.*
from room r
where not exists
(
select *
from meating_feature mf
left join room_feature rf on mf.id_feature = rf.id_feature and rf.id_room = r.id_room
where mf.id_meeting = 1
and rf.id_feature is null
);)解决这一问题还有其他方法。您可以选择用餐所需的房间专长,然后将每个房间找到的特征数量与所需功能的数量进行比较。
使用会议电话获取房间的查询更容易:
select *
from room
where id_room in
(
select id_room
from room_feature
where id_feature = (select id_feature from feature where name = 'conference phone')
);https://stackoverflow.com/questions/27044208
复制相似问题