2011-05-20 120 views
1

我有四個值表:教師,認證,block_subjects和塊以及兩個關係表:instructor_certifications和subject_certification。就像這樣:加入兩個多對多關係,'all'operator

block -- block_subject 
      | 
      | 
subject_certification 
      | 
      | 
    certification 
      | 
      | 
instructor_certification 
      | 
      | 
     instructor 

我想要一個查詢,會告訴我,每個塊,其中教官師資塊。具體來說,我不想指定塊ID作爲查詢的一部分;我想根據不同的標準選擇多個塊。

這裏是(非工作)查詢我目前有:

select inst.name, inst.id 
from instructor as inst 
join instructor_certification as ic on inst.id = ic.instructor_fid and 
ic.certification_fid = all (
    select cert.id 
    from block_subject as bs 
    join subject_certification as bsc on bsc.block_fid = bs.id 
    join certification as cert on bsc.certification_fid = cert.id 
    where bs.id = any (
     select bs.id 
    from block as b 
     join block_subject as bs on b.subject_fid = bs.id 
     where (b.start_date, b.end_date) overlaps (?, ?) 
    ) 
) 

顯然,這並不工作,因爲「所有」收集所有在日期範圍內的所有block_subject所需的認證。

編輯:另外我應該澄清,實際上,每個block_subject需要多個認證。

+0

聽起來像你對我要求的關係運算符是[師](http://www.simple-talk.com/sql/t-sql-編程/劃分我們的關係SQL的分支/):「提供所有部分的供應商」 - >「可以教授塊上所有主題的教師」,或許? – onedaywhen 2011-05-20 06:09:04

+0

這看起來像我需要的......哦,從來沒有真正採取數據庫類的樂趣。 – gibbss 2011-05-20 06:51:47

+0

好的,我將其更改爲新的要求。 – Hogan 2011-05-20 10:15:09

回答

1

編輯後:

如果要求每塊每名教師多證書:

select b.id as blockid, bs.subject_id as subject_id, i.id as inscructorid, count(ic.certification_id) as numCerts 
from block b 
join block_subject bs on b.id = bs.block_id 
join subject_certification sc on bs.subject_id = sc.subject_id 
join instructor_certification ic on sc.certification_id = ic.certification_id 
join instructor i on ic.instructor_id = i.id 
group by b.id, bs.subject_id, i.id 
having count(ic.certification_id) > 1 

您的問題,指出 「每塊」。所以,從塊開始,然後做連接。像這樣:

select * 
from block b 
join block_subject bs on b.id = bs.block_id 
join subject_certification sc on bs.subject_id = sc.subject_id 
join instructor_certification ic on sc.certification_id = ic.certification_id 
join instructor i on ic.instructor_id = i.id 

您現在可以添加任何您想要的標準。

特定塊?

日期範圍?

where @Date between b.start_date and b.end_date 

指導員?

where i.id = @inid 

認證?或者其組合。

0

給這個鏡頭。

declare @Date datetime 

set @Date = '01/01/2011' 

select inst.name, inst.id 
from instructor as inst 
join instructor_certification as ic on inst.id = ic.instructor_fid 
and ic.certification_fid in (
    select cert.id 
    from block_subject as bs 
    join subject_certification as bsc on bsc.block_fid = bs.id 
    join certification as cert on bsc.certification_fid = cert.id 
    where bs.id in (select bs.id 
     from block as b 
     join block_subject as bs on b.subject_fid = bs.id 
     where @Date between b.start_date and b.end_date)) 
    ) x 
0

只是教官......

select 
    i.* 
from 
    instructor i on 
where 
    exists (
    select * 
    from 
     block b 
     join block_subject bs on b.id = bs.block_id 
     join subject_certification sc on bs.subject_id = sc.subject_id 
     join instructor_certification ic on sc.certification_id = ic.certification_id 
     WHERE 
     ic.instructor_id = i.id 
     AND 
     ..other filters here 
     )