玩转C科技.NET

从学会做人开始认识这个世界!

导航

<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

统计

公告

Subscribe to this feed
Contact volnet online!

MSN群MyMSDN技术讨论群
群号:www.msdn@hotmail.com
Windows Live Alerts
LiveMessenger:
<My Library>

与我联系

搜索

 

常用链接

留言簿(6)

我参与的团队

我的标签

随笔分类(127)

随笔档案(114)

文章分类(15)

文章档案(15)

相册

家园建设

最新随笔

积分与排名

  • 积分 - 176632
  • 排名 - 213

最新评论

阅读排行榜

评论排行榜

60天内阅读排行

2007年11月23日 #

【代码保留】exists和not exists

代码功能:查询所有的是7或8并且不是4和5的项

select c.udt_usr_id usr_id, f.usr_name, c.udt_org_id org_id, e.org_shorname
 
from asm_useridentity c, ogm_organization e, asm_user f
 
where exists (select a.org_id
         
from ogm_organization a
        
where (a.org_codid = '7' or a.org_codid = '8')
          
and c.udt_org_id = a.org_id)
  
and not exists (select b.udt_usr_id
         
from ogm_organization a, asm_useridentity b
        
where (a.org_codid = '4' or a.org_codid = '5')
          
and b.udt_org_id = a.org_id
          
and c.udt_usr_id = b.udt_usr_id)
  
and exists (select c.udt_org_id
         
from ogm_organization d
        
where c.udt_org_id = d.org_id
        CONNECT
BY d.org_parentid = PRIOR d.org_id
         START
WITH d.org_id = '402')
  
and c.udt_org_id = e.org_id
  
and f.usr_id = c.udt_usr_id
 
group by udt_usr_id, udt_org_id, org_shorname, usr_name
 
order by org_id asc

后话:

exists和not exists与in和not in的区别:

1、效率上的,这一点google一下会有很多

2、使用上的区别:

由in转换成exists的情况:

在in前面通常有t.***id in (     select ***id from table1 a where *=*    )

转换则为:exists (    select ***id from table1 a where *=* and a.***id=t.***id     )

not exists对应not in只是在exists前加not,其余相同。

3、注意以上代码中or关键字,如果有or,通常需要在or的左右两侧的条件之间进行括号。而在in中通常使用与不使用效果相同,但在exists中则会有所区别。

posted @ 2007-11-23 16:44 volnet(可以叫我大V) 阅读(54) | 评论 (0)编辑

使用Live Messenger联系我
关闭