where中的条件分析

baisun 2007-12-04 09:24:49
如果aa表中AN<>''就用
select * from aa where AN<>'' and CN='123'
如果aa表中AN=''就用
select * from aa where CN='123'
把他们合成一个句子怎么写?
...全文
381 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
liujuns 2008-09-16
  • 打赏
  • 举报
回复
select * from tb
where
((AN <>'' and CN='123') or(CN='123') )
shuiniu 2007-12-06
  • 打赏
  • 举报
回复
同理,代码可以精简一点


select * from @table a where
cn = 123 and (an = all(select an from @table b where a.cn = b.cn) or an <> '')
shuiniu 2007-12-06
  • 打赏
  • 举报
回复

declare @table table(id int,an char(1),cn int)

insert @table
select 1,'a',121
insert @table
select 2,'',123
insert @table
select 3,'s',123

select * from @table a where (cn = 123 and an = all(select an from @table b where a.cn = b.cn))
or (cn = 123 and an <> '')

/*
id an cn
----------- ---- -----------
3 s 123
*/

delete from @table where id = 3

select * from @table a where (cn = 123 and an = all(select an from @table b where a.cn = b.cn))
or (cn = 123 and an <> '')
/*
id an cn
----------- ---- -----------
2 123
(1 row(s) affected)
*/
ainy360 2007-12-06
  • 打赏
  • 举报
回复
楼主和最上面的都很牛
xuebin801125 2007-12-06
  • 打赏
  • 举报
回复
樓主很有意思,不過一個語句可能辦不到啦!
天地会-爱人 2007-12-06
  • 打赏
  • 举报
回复
select * from a where (AN <> '' and CN='123' ) or (CN='123')
theskyworld 2007-12-05
  • 打赏
  • 举报
回复
select * from aa where AN <> '' and CN='123'
union
select * from aa where CN='123'
utpcb 2007-12-05
  • 打赏
  • 举报
回复
晕 要么AN<> '' 要么 AN=''是不是啊 这两个合起来不就是没有这个条件吗 那么不就是 select * from aa where CN='123' 我很奇怪楼主的意思
wzy_love_sly 2007-12-04
  • 打赏
  • 举报
回复
就是点他名字进他空间看啊 晕
-狙击手- 2007-12-04
  • 打赏
  • 举报
回复
web已经> 5000了


---------------------

wzy_love_sly ,什么 意思 ,在哪看这个呀?
wzy_love_sly 2007-12-04
  • 打赏
  • 举报
回复
我?比你差远了,天天向你们学习呢
hui_hui_2007 2007-12-04
  • 打赏
  • 举报
回复
帮顶一下,没看明白
-狙击手- 2007-12-04
  • 打赏
  • 举报
回复
是的,楼主牛,你也牛
wzy_love_sly 2007-12-04
  • 打赏
  • 举报
回复
果然是牛人,web已经>5000了 佩服
wzy_love_sly 2007-12-04
  • 打赏
  • 举报
回复
呵呵 他可真能搞 牛人
-狙击手- 2007-12-04
  • 打赏
  • 举报
回复
楼主要排它,貌似一句搞不定,得加判定
wzy_love_sly 2007-12-04
  • 打赏
  • 举报
回复
if(exists(select 1 from tb where an != '' and cn='123') ) 
exec('select * from tb where an !='''' and cn =''123''')
else
exec('select * from tb where an ='''' and cn =''123''')
wzy_love_sly 2007-12-04
  • 打赏
  • 举报
回复
drop table tb
create table tb (id int,AN varchar(8000),CN varchar(8000))
insert tb select 1,'a','121'
insert tb select 2,'','123'
insert tb select 3,'s','123'

create table tb (id int,AN varchar(8000),CN varchar(8000))
insert tb select 1,'a','121'
insert tb select 2,'','123'

if(exists(select 1 from tb where an != '' and cn='123') )
print '有'
else
print '无'

if(exists(select 1 from tb where an != '' and cn='123') )
exec('select * from tb where an !='''' and cn =''123''')
else


___________________________________________________________
3 s 123
___________________________________________________________
2 123



-狙击手- 2007-12-04
  • 打赏
  • 举报
回复
事实上,排除NULL那么,合并后就是第二句:
select * from aa where CN='123'

baisun 2007-12-04
  • 打赏
  • 举报
回复
比如,
如果表aa为
ID AN CN
1 a 121
2 123
3 s 123
...

查询结果要:
ID AN CN
3 s 123
-------------------------
如果表aa为
ID AN CN
1 a 121
2 123
...
查询结果要:
ID AN CN
2 123
加载更多回复(10)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧