我试图找到一个既有失眠又感冒的人。
到目前为止我尝试过的代码是:
SELECT Patient.patientName, Patient.patientPNbr
FROM Patient
LEFT JOIN SuffersFrom
ON Patient.patientPNbr = SuffersFrom.patientPNbr
WHERE diseaseName = 'Insomnia'
AND diseaseName = 'Influenza'
你会如何找到这个人?
答案 0 :(得分:3)
如果想要返回患有这两种疾病的患者,请尝试这样做
SELECT Patient.patientName, Patient.patientPNbr
FROM Patient
INNER JOIN SuffersFrom ON Patient.patientPNbr = SuffersFrom.patientPNbr
WHERE diseaseName IN ('Insomnia','Influenza')
GROUP BY Patient.patientName, Patient.patientPNbr
HAVING COUNT(diseaseName)>1
答案 1 :(得分:0)
您需要多次使用SuffersFrom
(每次检查疾病一次)。我会使用EXISTS
子句。
SELECT SELECT Patient.patientName, Patient.patientPNbr
FROM Patient
--Check for Insomnia
WHERE EXISTS (SELECT 1
FROM SuffersFrom
WHERE Patient.patientPNbr = SuffersFrom.patientPNbr
AND diseaseName = 'Insomnia')
--Check for Influenza
AND EXISTS (SELECT 1
FROM SuffersFrom
WHERE Patient.patientPNbr = SuffersFrom.patientPNbr
AND diseaseName = 'Influenza')
答案 2 :(得分:0)
使用以下查询加入...
SELECT P.patientName, P.patientPNbr
FROM Patient p
INNER JOIN SuffersFrom s
ON p.patientPNbr = s.patientPNbr
INNER JOIN SuffersFrom s1
ON s.patientPNbr = s1.patientPNbr
WHERE s.diseaseName = 'Insomnia'
AND s1.diseaseName = 'Influenza'