gpt4 book ai didi

sql - 将子查询转换为联接

转载 作者:可可西里 更新时间:2023-11-01 07:48:37 25 4
gpt4 key购买 nike

您好,是否可以将此子查询转换为联接?

SELECT staff_no
FROM doctor
WHERE NOT EXISTS (SELECT *
FROM patient
WHERE staff_no = consultant_no);

最佳答案

SELECT  staff_no
FROM doctor
LEFT JOIN
patient
ON staff_no = consultant_no
WHERE consultant_no IS NULL

为了提高效率,consultant_no 应该被索引并声明为 NOT NULL

如果不是,请在 patient 中选择任何声明为 NOT NULL 的列,并在 WHERE 子句中将 consultant_no 替换为该列。

请参阅我博客中的这篇文章,比较在 MySQL 中执行此查询的三种方法:

关于sql - 将子查询转换为联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1492154/

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