gpt4 book ai didi

SQL Join 和 Sub 查询混淆

转载 作者:行者123 更新时间:2023-12-04 23:48:28 26 4
gpt4 key购买 nike

SELECT familyname 
FROM dtoohey.patient, DTOOHEY.account, dtoohey.doctor
WHERE dtoohey.account.patientID = dtoohey.patient.patientid
AND dtoohey.doctor.providerno = DTOOHEY.account.providerno
AND dtoohey.doctor.name = 'Dr Brian';

SELECT familyname
FROM dtoohey.patient
WHERE patientid IN (
SELECT patientid
FROM dtoohey.account
WHERE providerno IN (
SELECT providerno
FROM dtoohey.doctor
WHERE name = 'Dr Brian'
)
);

这两者有什么区别?两者给出不同的结果。

最佳答案

执行计划的主要区别。可能这个查询会更有效 -

SELECT familyname
FROM dtoohey.patient p
WHERE EXISTS(
SELECT 1
FROM dtoohey.account a
JOIN dtoohey.doctor d ON d.providerno = a.providerno
WHERE a.patientID = p.patientid
AND d.Name = 'Dr Brian'
)

关于SQL Join 和 Sub 查询混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com