我建议创建另一个表,class_days
,其中包含每天一类将运行。
所以MWT
存储在days
而是将被存储为3行中单独的表
class_days
class_id, day
1,M
1,W
1,T
然后你可以使用not exists
选择不已安排了一天班没有一个给定的类用户(因此所有的类都被赋予天预定)
select c.id from classes c
where not exists (
select 1 from class_days cd
where cd.class_id = c.id
and cd.day not in ('M','W','T')
)
上面的查询假设在每一行210在class_days
中至少有1个相应的行。如果情况并非如此,请添加exists
条件以确保您不返回没有任何日期设置的课程。
select c.id from classes c
where not exists (
select 1 from class_days cd
where cd.class_id = c.id
and cd.day not in ('M','W','T')
) and exists (
select 1 from class_days cd
where cd.class_id = c.id
)
这些查询可以在class_days(class_id,day)
采取复合索引的优点是归一化/改变分贝结构的可能性? – FuzzyTree
是的,它仍在大力发展。你会建议什么 –