我建議創建另一個表,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
是的,它仍在大力發展。你會建議什麼 –