2009-07-21 157 views
0

非常感謝,再次提前。查詢問題

我所試圖做的是列表中,如果一個機構n使用在醫院H2 工程或具有 資格Q3或兩者

我有兩個表Agency_A &資格

Agency_A  
Nurse  Hospital 
Thomas  H1 
Taylor  H2 
Evans  H3 
Davies  H2 

Qualification 
Nurse  Qualification 
Thomas  Q2 
Taylor  Q3 
Evans  Q4 
Freeman Q3 

所以我'尋找

|TAYLOR |       {as she works in H2 and has qualification Q3} 
|FREEMAN |       {as she has qualification Q3} 
|DAVIES |       {as she works in H2 etc 

任何幫助,這將是偉大的。

回答

3

這應該覆蓋它:

SELECT 
    COALESCE(Qualification.Nurse, Agency_A.Nurse) 
FROM (
    SELECT DISTINCT Nurse 
    FROM Agency_A 
    WHERE Hospital = 'H2') Agency_A 
LEFT OUTER JOIN (
    SELECT DISTINCT Nurse 
    FROM Qualification 
    WHERE Qualification = 'Q3') Qualification 
ON Agency_A.Nurse = Qualification.Nurse 

這覆蓋的情況下護士是在一個表而不是其他。

+0

嗨,這是一個很大的幫助 - 非常感謝確實 - 再次感謝 – Ben 2009-07-21 15:22:55