关于where语句

riconyi 2005-11-02 03:57:38
select * from job where id=@id and jobid=@jobid and comid=@comid
现在的问题是
如果@jobid为空的话,我能否就用一个判断语句使and jobid=@jobid 不出现从而变成
select * from job where id=@id and comid=@comid
同样也有可能@comid也为空,或者@jobid和@comid都为空,那就变成
select * from job where id=@id
请问这种功能能利用innull函数实现吗?如果不能,还有别的办法没?
因为这里只举出了几种变量,另外还有好多,我不想在select语句前面判断再来确定where语句,那样子的话,如果我有6个变量,岂不是要判断6*6=36次?
...全文
147 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
riconyi 2005-11-03
  • 打赏
  • 举报
回复
up
riconyi 2005-11-02
  • 打赏
  • 举报
回复
看上去楼上的这种方法应该可以,但是为什么会没有任何数据呢?
vivianfdlpw 2005-11-02
  • 打赏
  • 举报
回复
select * from job
where id=isnull(@id,id)
and
jobid=isnull(@jobid,jobid)
and
comid=isnull(@comid,comid)
churchatp1 2005-11-02
  • 打赏
  • 举报
回复
试试看这样,肯定可以了:
select * from job where id=isnull(@id,id) and jobid=isnull(@jobid,jobid) and comid=(@comid,comid)
churchatp1 2005-11-02
  • 打赏
  • 举报
回复
select * from job where id=@id and jobid=(case when @jobid is null then jobid else @jobid end) and comid=(case when @comid is null then comid else @comid end )
试试看这样,这样应该可以吧
yesyesyes 2005-11-02
  • 打赏
  • 举报
回复
不行
冷箫轻笛 2005-11-02
  • 打赏
  • 举报
回复
这样行不行

select * from job where id=@id
and isnull(jobid,@jobid)=@jobid and isnull(comid,@jobid)= @jobid
冷箫轻笛 2005-11-02
  • 打赏
  • 举报
回复
这样行不行

select * from job where id=@id
and isnull(jobid,@jobid)=@jobid and isnull(comid,@jobid)= @jobid
nononono 2005-11-02
  • 打赏
  • 举报
回复
没有6*6那么多吧。这样行不?

select * from job where id=@id
and (@jobid is null or jobid=@jobid)
and (@comid is null or comid=@comid)

34,872

社区成员

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

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