欢迎大家访问我的BLOG,我会多多的出原创文章,希望大家支持我,为我祈祷,让我实现我的三个梦想!再30岁能成为一个名优秀的软件架构师!

请教关于“IN ”和 “EXISTS”的问题

作者
主题    发布新主题    回复主题
feifei5520
初级会员

注册日期: 2006 May
来自:
技术贴数:3
论坛积分:9
论坛排名:71865
论坛徽章:0

           
           

高兴 请教关于“IN ”和 “EXISTS”的问题

小弟有个问题百思不得其解
我现在有个这样的语句:

select *
from goods
where code in (select code from goods where code='01010002')
其中“code”为主码,所以执行结果只存在一条记录;

我现在想把他变成 EXISTS的形式 如下:

select *
from goods
where exists
(select *from goods where code='01010002')

可是执行出结果有N条记录,为什么会不一样呢?

我知道肯定是我的方法不对,但是不对在哪里呢?
有请大虾指点一二,不甚感激!

 

 

向版主反映这个帖子  查看feifei5520 的IP地址

楼主 旧帖 06-06-01 17:20
[ ]    给予该贴好评   编辑/删除 引用/回复
feifei5520
初级会员

注册日期: 2006 May
来自:
技术贴数:3
论坛积分:9
论坛排名:71865
论坛徽章:0

           
           

wait^^^^^^

wait^^^^^^

 

 

向版主反映这个帖子  查看feifei5520 的IP地址

2楼 旧帖 06-06-01 18:14
[ ]    给予该贴好评   编辑/删除 引用/回复
flywolf2000
飞狼

注册日期: 2004 Feb
来自: 天狼星
技术贴数:1966
论坛积分:9965
论坛排名:195
论坛徽章:5

现任管理团队成员 管理团队2006纪念徽章 会员2006贡献徽章 授权会员 数据库板块每日发贴之星  
           

USE pubs
GO
SELECT DISTINCT pub_name
FROM publishers
WHERE EXISTS
(SELECT *
FROM titles
WHERE pub_id = publishers.pub_id
AND type = 'business')
GO

-- Or, using the IN clause:

USE pubs
GO
SELECT distinct pub_name
FROM publishers
WHERE pub_id IN
(SELECT pub_id
FROM titles
WHERE type = 'business')
GO




这两个语句才是一样的,你那个不对

 

 

向版主反映这个帖子  查看flywolf2000 的IP地址

3楼 旧帖 06-06-01 18:18
[ ]    给予该贴好评   编辑/删除 引用/回复
feifei5520
初级会员

注册日期: 2006 May
来自:
技术贴数:3
论坛积分:9
论坛排名:71865
论坛徽章:0

           
           

首先感谢二楼的兄弟
不过我想不明白的是为什么我的语句会错,错在哪里.这个是我更想知道的、
如果要把我上面所写的“IN子句”形式转换为“EXISTS”子句该怎么写呢?
如果不能转换,又是为什么呢?

 

 

向版主反映这个帖子  查看feifei5520 的IP地址

4楼 旧帖 06-06-01 18:58
[ ]    给予该贴好评   编辑/删除 引用/回复
smthgdin
一般会员

注册日期: 2004 May
来自: 广州
技术贴数:38
论坛积分:113
论坛排名:13090
论坛徽章:0

           
           

Re: 请教关于“IN ”和 “EXISTS”的问题

quote:
最初由 feifei5520 发布
小弟有个问题百思不得其解
我现在有个这样的语句:

select *
from goods
where code in (select code from goods where code='01010002')
其中“code”为主码,所以执行结果只存在一条记录;

我现在想把他变成 EXISTS的形式 如下:

select *
from goods
where exists
(select *from goods where code='01010002')

可是执行出结果有N条记录,为什么会不一样呢?

我知道肯定是我的方法不对,但是不对在哪里呢?
有请大虾指点一二,不甚感激!



我的理解是exists (select *from goods where code='01010002')对于表goods所有的记录都返回true值,所以会得到n个记录.
问题出在select *from goods where code='01010002' 一直是true.

可以看下面的资料:

在ASKTOM的讲解:
Well, the two are processed very very differently.

Select * from T1 where x in ( select y from T2 )

is typically processed as:

select *
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;

The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then
joined to the original table -- typically.


As opposed to

select * from t1 where exists ( select null from t2 where y = x )

That is processed more like:


for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD
end if
end loop

It always results in a full scan of T1 whereas the first query can make use of
an index on T1(x).


So, when is where exists appropriate and in appropriate?

Lets say the result of the subquery
( select y from T2 )

is "huge" and takes a long time. But the table T1 is relatively small and
executing ( select null from t2 where y = x.x ) is very very fast (nice index on
t2(y)). Then the exists will be faster as the time to full scan T1 and do the
index probe into T2 could be less then the time to simply full scan T2 to build
the subquery we need to distinct on.


Lets say the result of the subquery is small -- then IN is typicaly more
appropriate.


If both the subquery and the outer table are huge -- either might work as well
as the other -- depends on the indexes and other factors.

 


__________________
www.kooboy.net

 

向版主反映这个帖子  查看smthgdin 的IP地址

5楼 旧帖 06-06-01 21:10
[ ]    给予该贴好评   编辑/删除 引用/回复
feifei5520
初级会员

注册日期: 2006 May
来自:
技术贴数:3
论坛积分:9
论坛排名:71865
论坛徽章:0

           
           

嗯,好像是那么回事,多谢楼上的
对了你这个“select * from t1 where exists ( select null from t2 where y = x )”里面的“null”好像是个关键字,在这里它代表什么?

还有一个问题就是:如果为了使子句不恒为TURE值的话,
是不是在子查询中必须涉及到两个表的连接才行?

正如上面的这个语句:
select *
from goods
where code in (select code from goods where code='01010002')

由于查询中只牵涉到单个表,所以无法转换为EXISTS的形式,对吗?

 

由 feifei5520 于 06-06-02 09:34 最后编辑

向版主反映这个帖子  查看feifei5520 的IP地址

6楼 旧帖 06-06-02 08:38
[ ]    给予该贴好评   编辑/删除 引用/回复
smthgdin
一般会员

注册日期: 2004 May
来自: 广州
技术贴数:38
论坛积分:113
论坛排名:13090
论坛徽章:0

           
           

quote:
最初由 feifei5520 发布
嗯,好像是那么回事,多谢楼上的
对了你这个“select * from t1 where exists ( select null from t2 where y = x )”里面的“null”好像是个关键字,在这里它代表什么?

还有一个问题就是:如果为了使子句不恒为TURE值的话,
是不是在子查询中必须涉及到两个表的连接才行?

正如上面的这个语句:
select *
from goods
where code in (select code from goods where code='01010002')

由于查询中只牵涉到单个表,所以无法转换为EXISTS的形式,对吗?



对,你要关联外部的那张表,才不会一直为true.
select *
from goods t1
where exists(select code from goods t2 where t1.code='01010002')

 


__________________
www.kooboy.net

 

向版主反映这个帖子  查看smthgdin 的IP地址

7楼 新帖 06-06-02 10:00
[ ]    给予该贴好评   编辑/删除 引用/回复
xiaodong_1567
中级会员

注册日期: 2005 Jun
来自:
技术贴数:160
论坛积分:397
论坛排名:4585
论坛徽章:0

           
           

学到不少!!!

 

 

向版主反映这个帖子  查看xiaodong_1567 的IP地址

8楼 新帖 06-06-02 10:30
[ ]    给予该贴好评   编辑/删除 引用/回复
ASP_NET
初级会员

注册日期: 2005 Mar
来自: 东莞
技术贴数:22
论坛积分:66
论坛排名:19306
论坛徽章:0

           
           

楼主去看SQL帮助文件查EXISTS的使用方法就知道了。
EXISTS,如果子查询包含行,则返回 TRUE,只是判断是否存在行记录。
如:
select * from goods t1 where exists(select code from goods t2 where t1.code='01010002')
与 select * from goods t1 where code='01010002'一样,
从速度,简洁上看,select * from goods t1 where code='01010002'比较好。

当我们一定要用到exists时候,
select * from goods t1 where exists(select code from goods t2 where t1.code='01010002')

select * from goods t1 where exists(select 1 from goods t2 where t1.code='01010002')
速度慢。(注:当数据量很大,查询语句非常复杂时候就可以看出来了)


__________________
目标:做一名高级程序员
爱好:编程 游戏 旅游
【No incapable except unthinkable】

 

向版主反映这个帖子  查看ASP_NET 的IP地址

9楼 新帖 06-06-02 13:13
[ ]    给予该贴好评   编辑/删除 引用/回复
feifei5520
初级会员

注册日期: 2006 May
来自:
技术贴数:3
论坛积分:9
论坛排名:71865
论坛徽章:0

           
           

呵呵 多谢各位, 明白不少!

 

 

向版主反映这个帖子

posted on 2006-07-12 14:01  程序缘  阅读(548)  评论(0)    收藏  举报

导航