我在Access中有一个小型数据库,除了以下问题之外,它运行良好.数据库设计正确,任何其他100个查询我工作正常.我正在尝试找到正确的SQL来返回正确的记录.肯定有记录符合(((tblComorbidity.comorbidityexplanation)="感染"和(tblComorbidity.comorbidityexplanation)="压疮")); 部分,但由于某种原因,它什么都不返回.有什么建议?谢谢
SELECT DISTINCT Person.PersonID, tblComorbidity.comorbidityexplanation
FROM
tblKentuckyCounties INNER JOIN
(tblComorbidity INNER JOIN (Person INNER JOIN tblComorbidityPerson ON Person.PersonID = tblComorbidityPerson.personID) ON tblComorbidity.ID = tblComorbidityPerson.comorbidityFK) ON tblKentuckyCounties.ID = Person.County
WHERE
(((tblComorbidity.comorbidityexplanation)="infection" And (tblComorbidity.comorbidityexplanation)="pressure sores"));
Run Code Online (Sandbox Code Playgroud)
您的SQL正在尝试获取所有tblComorbidity记录,其中包含"感染"和"压疮"的解释.这是不可能的,因为ONE tblComorbidity记录只有一个解释.
我真正想要的是得到所有人,其中一个tblComorbidity记录与解释="感染"存在,另一个tblComorbidity记录与解释="压疮"存在.听起来很相似,但它有所不同,需要以不同的方式完成.
像下面的SQL应该做你想要的:
SELECT DISTINCT Person.PersonID
FROM ((((Person INNER JOIN tblComorbidityPerson AS InfPers ON Person.PersonID = InfPers.PersonID)
INNER JOIN tblComorbidity AS Inf ON InfPers.comorbidityFK = Inf.ID)
INNER JOIN tblComorbidityPerson AS SorePers ON Person.PersonID = SorePers.PersonID)
INNER JOIN tblComorbidity AS Sore ON SorePers.comorbidityFK = Sore.ID)
INNER JOIN tblKentuckyCounties ON tblKentuckyCounties.ID = Person.County
WHERE Inf.comorbidityexplanation = "infection"
AND Sore.comorbidityexplanation = "pressure sores";
Run Code Online (Sandbox Code Playgroud)
基本上,你需要加入合并症两次(每次解释一次).
| 归档时间: |
|
| 查看次数: |
206 次 |
| 最近记录: |