电商爬爬
电商爬爬
3月前 · 3 人阅读

select 1 from b where b.id = a.id和 select 1 from a where b.id = a.id有区别么?

请问他们有区别么?

举个例子:

select * from b where exists (select 1 from a where a.c=b.c)

select * from b where exists (select 1 from b where a.c=b.c)

这两句有区别么?(高效)SELECT * FROM EMP (基础表) WHERE EMPNO > 0 AND EXISTS (SELECT ‘X’ FROM DEPT WHERE DEPT.DEPTNO = EMP.DEPTNO AND LOC = ‘MELB’)

(低效)SELECT * FROM EMP (基础表) WHERE EMPNO > 0 AND DEPTNO IN(SELECT DEPTNO FROM DEPT WHERE LOC = ‘MELB’)

选的表不同,你的例子,第一个是从b表选a.c=b.c的数据,第二个是从a表选。就好像a有3个c=1,b有1个c=1,你说从不同表选c=1结果一样不?就这意思。

其实你的:select * from b where exists (select 1 from a where a.c=b.c)

等价于:select * from b where b.c IN (select a.c from a where a.c=b.c)

可能用下面这句容易看懂些。就是从b表中查找与a表有相同字段c的所有结果集。

收藏 0
关键词: select where id select where d
评论