求一sql语句,当列值为空不作查询条件,不为空作为查询条件!

tianshimeng724 2008-10-30 04:45:51
例如:
select * from usertable where name=@name and page=@page
我的要求是当name为空就不作为条件,即:
select * from usertable where page=@page
当page为空不作为条件,即:
select * from usertable where name=@name

怎么用一条sql语句写出来???

如果我想name和page都为空,都不作为条件呢?能用一条sql语句写出来吗?
...全文
12594 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
huawenzi750 2010-03-30
  • 打赏
  • 举报
回复
8楼不写错字的确是对的。 在VC里,写 '%s' ='' 不能用'%s' is null
huawenzi750 2010-03-30
  • 打赏
  • 举报
回复
还是不会
tianshimeng724 2009-03-09
  • 打赏
  • 举报
回复
8楼,ok
tianshimeng724 2008-12-30
  • 打赏
  • 举报
回复
COALESCE()
  • 打赏
  • 举报
回复
你一个一个的试一下,不就行了。上面的基本都正确
tianshimeng724 2008-10-30
  • 打赏
  • 举报
回复
SELECT * FROM proj_manage WHERE charge=IFNULL(NULLIF('1',''),charge) AND proj_name=IFNULL(NULLIF('2222',''),proj_name)
结贴了
-晴天 2008-10-30
  • 打赏
  • 举报
回复 1
create table tb(id int,n varchar(10),page int)
insert into tb select 1,'aa',5
insert into tb select 1,'bb',3
go
declare @n varchar(10),@p int
--set @n='aa'
set @p=5
--下面是你要的一句sql语句
select * from tb where 1=(case when @n is null or n=@n then 1 else 0 end) and 1=(case when @p is null or page=@p then 1 else 0 end)
go
drop table tb
/*
id n page
----------- ---------- -----------
1 aa 5
*/
fa_ge 2008-10-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 tianshimeng724 的回复:]
首先谢谢各位,但回了那么多,我都不知道那个是对的了,我再详细的解释一下:
我说的空是指“”,空字符串!!(之前没说清楚)
1、如果用户两个都输入1、1,则sql为:
select * from usertable where name='1' and page=‘1’
2、如果用户第一个都输入1,没输入第二个,则sql为:
select * from usertable where name='1'
3、如果用户第二个都输入1,没输入第一个,则sql为:
select * from usertable where page=…
[/Quote]

不是回答得很清楚嘛
tianshimeng724 2008-10-30
  • 打赏
  • 举报
回复
首先谢谢各位,但回了那么多,我都不知道那个是对的了,我再详细的解释一下:
我说的空是指“”,空字符串!!(之前没说清楚)
1、如果用户两个都输入1、1,则sql为:
select * from usertable where name='1' and page=‘1’
2、如果用户第一个都输入1,没输入第二个,则sql为:
select * from usertable where name='1'
3、如果用户第二个都输入1,没输入第一个,则sql为:
select * from usertable where page=‘1‘
4、如果用户都没输入,则sql为:
select * from usertable

上面的sql是我要实现的效果,
怎么用一条sql语句写出来???
  • 打赏
  • 举报
回复
1、select * from tb where (@name id null or name=@name ) and (page is null or page=@page )

fcuandy 2008-10-30
  • 打赏
  • 举报
回复
SELECT * FROM userTable WHERE name=ISNULL(@name,name) AND page=ISNULL(@page,page)
fa_ge 2008-10-30
  • 打赏
  • 举报
回复

select * from usertable
where name like '%'+@name+'%' and page like '%'+@page+'%'

--只要有一个不为空它就是查询条件,如果都为空的话就相当于 select * from usertable

Liyingyue_FFS 2008-10-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tianshimeng724 的回复:]
我的意思是@name、@page用户输入的值为空不作为查询条件
[/Quote]

SELECT * FROM usertable WHERE name=ISNULL(NULLIF(@name,''),name) AND page=ISNULL(NULLIF(@page,''),page)
Liyingyue_FFS 2008-10-30
  • 打赏
  • 举报
回复
select * from usertable 
where (name=@name and page=@page ) or (page=@page and name is null) or (name=@name and page is null) or (page is null and name is null)
csdyyr 2008-10-30
  • 打赏
  • 举报
回复

select * from usertable
where (name=@name and page=@page ) or @name is null or @page is null

csdyyr 2008-10-30
  • 打赏
  • 举报
回复 1

select * from usertable
where (name=@name and page=@page ) or name is null or page is null
tianshimeng724 2008-10-30
  • 打赏
  • 举报
回复
我的意思是@name、@page用户输入的值为空不作为查询条件

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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