这个sql怎么写

SELECT  *  FROM bigdata.GLR_B_ORG_INFO

 where 

 

 (org_cd=(select  org_cd  from   bigdata.GLR_B_ORG_INFO

          where  org_id= '${参数}')  

 

 or 

 

parent_org_cd =(select  org_cd  from   bigdata.GLR_B_ORG_INFO

 

          where   org_id='${参数}'))

   注释:WHERE 后面加个判断  当a=1的时候正常执行 上面的where 后面条件   当a=2的时候    让 org_cd='101021011' OR parent_org_cd='101000001'

FineReport 15764373317 发布于 2021-3-23 13:15 (编辑于 2021-3-23 13:57)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共4回答
最佳回答
0
fofantasyLv5初级互助
发布于2021-3-23 13:21(编辑于 2021-3-23 16:55)

SELECT   *   FROM bigdata.GLR_B_ORG_INFO 

WHERE 1=1  

${if(a=1," AND (org_cd=(select org_cd  from   bigdata.GLR_B_ORG_INFO where  org_id= '"+参数+"') or parent_org_cd =(select org_cd  from  bigdata.GLR_B_ORG_INFO where   org_id='"+参数+"'))"," AND (org_cd='101021011' OR parent_org_cd='101000001')")}

  • 15764373317 15764373317(提问者) 能整体的写一下嘛 就是a=1的时候 执行现在where 后面的 a=2的时候 执行org_cd=\\\'101021011\\\' OR parent_org_cd=\\\'101000001\\\'
    2021-03-23 13:22 
  • 15764373317 15764373317(提问者) 能整体把where 后面的提供一下嘛 谢谢了
    2021-03-23 13:24 
  • fofantasy fofantasy 回复 15764373317(提问者) 更新了,你看下,是你想要的不
    2021-03-23 13:29 
  • 15764373317 15764373317(提问者) 回复 fofantasy 能加下我qq 吗1945000388 还是不对
    2021-03-23 13:32 
  • fofantasy fofantasy 回复 15764373317(提问者) 我有仔细看了一遍你的需求,原来你是这个意思,重新更新了SQL,你再试下一啊
    2021-03-23 13:49 
最佳回答
0
shirokoLv6资深互助
发布于2021-3-23 13:19

where 1=1

${IF(a=1,"","and ( org_cd='101021011' OR parent_org_cd='101000001')")}

--

另外你的代码里公式都没有加${},应该不对吧

  • 15764373317 15764373317(提问者) 整体的能写一下吗 就是a=1的时候 执行现在where 后面的 a=2的时候 执行org_cd=\'101021011\' OR parent_org_cd=\'101000001\'
    2021-03-23 13:21 
  • 15764373317 15764373317(提问者) 能整体把where 后面的提供一下嘛 谢谢了
    2021-03-23 13:24 
最佳回答
0
坚果联盟Lv4见习互助
发布于2021-3-23 13:49

你这里的a是参数,会代入到代码里面去,如果a=1,那where后面就是

(org_cd=(select  org_cd  from   bigdata.GLR_B_ORG_INFO

          where  org_id= '1')  

 

 or 

 

parent_org_cd =(select  org_cd  from   bigdata.GLR_B_ORG_INFO

 

          where   org_id='1'))

这样的条件合理吗?你是想用哪个做参数?a=1和a=2时的条件是固定的吗?

你把a=1时的条件写出来一下

  • 15764373317 15764373317(提问者) a =1 是固定的条件
    2021-03-23 13:51 
  • 15764373317 15764373317(提问者) SELECT * FROM bigdata.GLR_B_ORG_INFO where 1=1 ${if(a=1,\"AND (org_cd=\'\"+(select org_cd from bigdata.GLR_B_ORG_INFO where org_id= \'长春朝阳和润村镇银行核算中心\') or parent_org_cd=(select org_cd from bigdata.GLR_B_ORG_INFO where org_id=\'长春朝阳和润村镇银行核算中心\'))+\'\" \",\"and ( parent_org_cd=\'101000001\')\")}
    2021-03-23 13:53 
  • 15764373317 15764373317(提问者) 我这个都固定写死了 就是 a=1执行 后面的 否则就在后面的 但是看or 好像语法错误
    2021-03-23 13:54 
最佳回答
0
zsh331Lv8专家互助
发布于2021-3-23 21:26
SELECT *   FROM bigdata.GLR_B_ORG_INFO  where 1=1    ${if(sql("数据连接名","SELECT flag FROM  bigdata.GLR_B_ORG_INFO  WHERE 条件字段 ='"+a+"'",1,1)=1," and (org_cd=(select org_cd from bigdata.GLR_B_ORG_INFO where  org_id= '"+a+"') or parent_org_cd =(select  org_cd  from   bigdata.GLR_B_ORG_INFO where org_id='"+a+"'))"," and flag = 1")}

  • 5关注人数
  • 515浏览人数
  • 最后回答于:2021-3-23 21:26
    请选择关闭问题的原因
    确定 取消
    返回顶部