什么时候用 in ?什么时候用 exists ?

来自姬鸿昌的知识库
Jihongchang讨论 | 贡献2023年8月15日 (二) 02:09的版本
跳到导航 跳到搜索

示例

select name 
from employees
where empno in
    (select distinct empno from job_history);
select name
from employees
where exists
    (select empno from job_history where employees.empno=job_history.empno);

如果 employees 中记录数大于 job_history 时,用 in 效率高。

如果 employees 中记录数小于 job_history 时,用 exists 效率高。