查询问题

时间:2009-07-21 15:08:18

标签: mysql

再次提前感谢。

我想要做的是列出一个代理人在H2 ​​的医院工作 资格Q3或两者

我有两张桌子代理商_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

任何帮助都会很棒。

1 个答案:

答案 0 :(得分: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

这包括护士在一张桌子而不是另一张桌子的情况。