【发布时间】:2015-12-11 04:02:01
【问题描述】:
我有 3 张表 Doctor、Patient 和 Visit。
Doctor Table 包含 DoctorID、Name 和 City。
Patient Table 包含 PatientID、Name 和 City。
访问表有 DoctorID、PatientID、NumVisits。
我正在尝试检索某个城市(比方说纽约)的患者未曾就诊的所有医生。
我在编写查询方面非常陌生,但我似乎无法让它发挥作用。
我的代码:
SELECT DoctorId,
Doctor.Name
FROM Visit
JOIN Doctor using(DoctorID)
JOIN Patient using(PatientID)
WHERE NOT EXISTS
(SELECT DoctorId,
Doctor.Name
FROM Visit
JOIN Doctor using(DoctorID)
JOIN Patient using(PatientID)
WHERE Patient.City = 'New York');
有人可以向我解释我做错了什么吗?也许我的整个方法都不正确。
【问题讨论】:
-
您绝对不需要 6 个查询来完成此操作!退后一步从不同的角度考虑会有所帮助,一个很好的经验法则是,您应该
never执行比查询中涉及的表更多的连接!!!
标签: sql not-exists