“什么时候用 in ?什么时候用 exists ?”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“1”的新页面)
 
第1行: 第1行:
1
+
=== 示例 ===
 +
<syntaxhighlight lang="sql">
 +
select name
 +
from employees
 +
where empno in
 +
    (select distinct empno from job_history);
 +
</syntaxhighlight><syntaxhighlight lang="sql">
 +
select name
 +
from employees
 +
where exists
 +
    (select empno from job_history where employees.empno=job_history.empno);
 +
</syntaxhighlight>如果 employees 中记录数大于 job_history 时,用 in 效率高。
 +
 
 +
如果 employees 中记录数小于 job_history 时,用 exists 效率高。

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 效率高。