“什么时候用 in ?什么时候用 exists ?”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
Jihongchang(讨论 | 贡献)  (建立内容为“1”的新页面)  | 
				Jihongchang(讨论 | 贡献)   | 
				||
| 第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 效率高。