一句sql解决多条件查询的语句怎么写?

usun82 2010-04-06 03:03:42
表A有pname,tel,email等3个字段,现在是存储过程接收3个对应的参数@pname,@tel,@email
当@pname='0'时,想有这样查询效果 select * from A where tel=@tel and email =@email
当@tel ='0'时,想有这样查询效果 select * from A where pname=@pnameand email =@email
当@email='0'时,想有这样查询效果 select * from A where tel=@tel and pname=@pname
当3个参数都为0时,想有这样查询效果 select * from A
请问用一句话的sql怎么写?谢谢!!!
...全文
372 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yujunlin32167 2010-04-06
  • 打赏
  • 举报
回复
学习一下....
usun82 2010-04-06
  • 打赏
  • 举报
回复
谢谢哈!!!
htl258_Tony 2010-04-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 htl258 的回复:]
SQL code
select * from A where
email=case when isnull(@email,'0')='0' then email else @email end and
pname=case when isnull(@pname,'0')='0' then pname else @pname end and
tel=case when isnull(@te……
[/Quote]
测试一下,这是我常用的.应该可以达到要求.
sxiaohui8709253 2010-04-06
  • 打赏
  • 举报
回复
学习 接分
htl258_Tony 2010-04-06
  • 打赏
  • 举报
回复
select * from A where 
email=case when isnull(@email,'0')='0' then email else @email end and
pname=case when isnull(@pname,'0')='0' then pname else @pname end and
tel=case when isnull(@tel,'0')='0' then tel else @tel end
最好加个NULL判断
dawugui 2010-04-06
  • 打赏
  • 举报
回复

begin
if @pname='0'
select * from A where tel=@tel and email =@email
else
begin
if @tel ='0'
select * from A where pname=@pnameand email =@email
else
begin
if @email='0'
select * from A where tel=@tel and pname=@pname
else
select * from A
end
end
end
--小F-- 2010-04-06
  • 打赏
  • 举报
回复
if (@pname='0')
select * from A where tel=@tel and email =@email
else if(@tel ='0')
....
else if(@email='0')
else if...
...
htl258_Tony 2010-04-06
  • 打赏
  • 举报
回复
select * from A where 
email=case when @email='0' then email else @email end and
pname=case when @pname='0' then pname else @pname end and
tel=case when @tel='0' then tel else @tel end
dawugui 2010-04-06
  • 打赏
  • 举报
回复
在存储过程中用if else去完成.
usun82 2010-04-06
  • 打赏
  • 举报
回复
我的现在是在存储过程中写,不适合拼写字符串。

34,873

社区成员

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

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