两个空白显示全部怎么加括号

我的sql语句

select * from table where 1=1

(${if(len(a)==0,"","and num >= '"+a+"'")}

${if(len(b)==0,"","and num <= '"+b+"'")})

(${if(len(c)==0,"","and count >= '"+c+"'")}

${if(len(d)==0,"","and count >= '"+d+"'")})

and name = '${name}' and zhiwu = '${zhiwu}'

我这样加括号就是语法错误。

想实现的功能是

1.当a、b两个参数有值时,num取在a、b两个数之前的数据;

2.当c、d两个参数有值时,count取在c、d两个数之前的数据;

3.当a、b、c、d都有值时,把1、2两个条件用or连接

sql应该怎么写,求教!!!

392380857 发布于 2022-3-27 08:35
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共3回答
最佳回答
0
15922204585Lv6高级互助
发布于2022-3-27 09:23

select * from 

(select 10 as num,6 as soc ) aa

where 1=1

${if(len(a)==0,if(len(b)==0,"",""),"and ( num >= "+a+"  ")}

--

${if(len(b)==0,if(len(a)==0,"",")"),if(len(a)=0,"and ( num <= "+b+" ) ","and num <= "+b+" ) "))}

${if(len(c)==0,if(len(d)==0,"","and ("),if(len(a)=0&&len(b)=0,"and ( soc >= "+c+"  ","or ( soc >= "+c+"  "))}

${if(len(d)==0,if(len(c)==0,"",")"),if(len(c)==0," soc <= "+d+" )","and soc <= "+d+" )"))}

image.png

最佳回答
0
CD20160914Lv8专家互助
发布于2022-3-27 09:46

select * from table 

where 1=1

${if(len(a)>0&&len(b)>0&&len(c)>0&&len(d)>0,"and(num>='"+a+"' and num<='"+b+"') 

or (count>='"+c+"' and count<='"+d+"')",

if(len(a)>0&&len(b)>0," and num>='"+a+"' and num<='"+b+"'",

if(len(c)>0&&len(d)>0," and count>='"+c+"' and count<='"+d+"'","")))}

and name = '${name}' 

and zhiwu = '${zhiwu}'

最佳回答
0
shinger@126.comLv2见习互助
发布于2022-4-19 10:55

and前面要加空格 不然会拼接成 where 1=1and num>='2' 这个样子,1和and之间没有空格,当然会报语法错误

  • 5关注人数
  • 416浏览人数
  • 最后回答于:2022-4-19 10:55
    请选择关闭问题的原因
    确定 取消
    返回顶部