在oralce中求一sql 分别用case 与 decode 实现

快跑蜗牛哥 2016-08-08 06:41:34
三个表简单条件关联,
页面上 参数 传到 sql ,
根据不用参数,sql条件不同。(直接在sql中实现,不在页面上判断)
获得两个参数。
当 第1个参数值为空 / "" 与 第2个参数为 空 / ""时,条件 rownum<1
当 第1个参数值为空 / "" 第2个参数不为空 / ""时, 条件 instr('adcdsdd',bb.btype)>0
其中 adcdsdd 是获得的第2个参数 值
当 第2个参数值为空 / "" 第1个参数不为空 / ""时, 条件 instr('fffdsssdddd',aa.atype)>0
其中 fffdsssdddd 是获得的第1个参数 值
当 第1个参数值不为空 / "" 第2个参数不为空 / ""时, 条件 instr('adcdsdd',bb.btype)>0 or instr('fffdsssdddd',aa.atype)>0
其中 adcdsdd 是获得的第2个参数 值 fffdsssdddd 是获得的第1个参数 值

求一个sql
...全文
215 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnwz 2016-08-18
  • 打赏
  • 举报
回复
这样?
SELECT  a.ty,a.itemno,a.qty
FROM  a,b
WHERE a.atype=b.btype
and a.packno<>'0' 
  and ((para1 is null and para2 is null and rownum<1)
       or (para1 is null and para2 is not null and instr(para2,b.btype)>0)
       or (para1 is not null and para2 is null and instr(para1,a.atype)>0)
       or (para1 is not null and para2 is not null and (instr(para2,b.btype)>0 or instr(para1,a.atype)>0))
ORDER BY a.ty ASC;
大表的话性能不好的,如果可以还是拼接动态SQL吧
快跑蜗牛哥 2016-08-09
  • 打赏
  • 举报
回复
引用 1 楼 ghx287524027 的回复:
select 
case when para1 is null     and para2 is null     then ' rownum<1'
	 when para1 is null     and para2 is not null then ' instr(para2,bb.btype)>0'
	 when para1 is not null and para2 is null     then ' instr(para1,aa.atype)>0'
	 else '  instr(para2,bb.btype)>0 or  instr(para1,aa.atype)>0'
end tj into v_tj
from dual;

v_where:=v_where||' and '||v_tj
大概意思这样
如下 sql 该如何修改?
SELECT  a.ty,a.itemno,a.qty
   FROM  a,b
  WHERE a.atype=b.btype
and a.packno<>'0' and (CASE para1 is null
                THEN ' rownum<1'
              ELSE 'instr(para1,atype)>0' 
           END)  ORDER BY a.ty ASC;
快跑蜗牛哥 2016-08-09
  • 打赏
  • 举报
回复
引用 1 楼 ghx287524027 的回复:
select 
case when para1 is null     and para2 is null     then ' rownum<1'
	 when para1 is null     and para2 is not null then ' instr(para2,bb.btype)>0'
	 when para1 is not null and para2 is null     then ' instr(para1,aa.atype)>0'
	 else '  instr(para2,bb.btype)>0 or  instr(para1,aa.atype)>0'
end tj into v_tj
from dual;

v_where:=v_where||' and '||v_tj
大概意思这样
你好! case 可以写在 where 条件中吗? 如何修改
快跑蜗牛哥 2016-08-09
  • 打赏
  • 举报
回复
引用 2 楼 wmxcn2000 的回复:
这两个参数,前台传过来的是什么 呢?
字符串
卖水果的net 2016-08-08
  • 打赏
  • 举报
回复
这两个参数,前台传过来的是什么 呢?
ghx287524027 2016-08-08
  • 打赏
  • 举报
回复
select 
case when para1 is null     and para2 is null     then ' rownum<1'
	 when para1 is null     and para2 is not null then ' instr(para2,bb.btype)>0'
	 when para1 is not null and para2 is null     then ' instr(para1,aa.atype)>0'
	 else '  instr(para2,bb.btype)>0 or  instr(para1,aa.atype)>0'
end tj into v_tj
from dual;

v_where:=v_where||' and '||v_tj
大概意思这样

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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