${if()}使用

${if(e==0,

"

select 

t3.office_name,

t1.total_spec_gene_rate `今年`,

t2.total_spec_gene_rate `去年`,

t1.freight_yoy `同比`

from 

(

select distinct office_code,office_name from freight_sales_amount_rate_analyze_day

) t3 left join

(

SELECT data_day,office_name,office_code,

ifnull(total_spec_gene_rate,0) total_spec_gene_rate,

ifnull(freight_yoy,0)-1 as `freight_yoy`

FROM `freight_sales_amount_rate_analyze_day`  

where 

"+if(len(a)=0,'1=1',' distribution_center_name  = '+a+'')+"

and  data_day =   '"+b+"' 

group by office_name,office_code

) t1 on t1.office_code=t3.office_code left join

(

SELECT data_day,office_name,office_code,

ifnull(total_spec_gene_rate,0) total_spec_gene_rate

FROM `freight_sales_amount_rate_analyze_day`  

where 

"+if(len(a)=0,'1=1',' distribution_center_name  = '+a+'')+"

and  data_day = date_sub('"+b+"' ,interval 1 year)

group by office_name,office_code

) t2 on t2.office_code=t3.office_code

"

,"")}

在${if()}

里面怎么使用if函数

现在这么写报错

image.png

pengfuqiang96 发布于 2021-6-1 11:53 (编辑于 2021-6-1 14:41)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共4回答
最佳回答
0
snrtuemcLv8专家互助
发布于2021-6-1 12:26(编辑于 2021-6-1 14:51)

修改答案

---------------

${if(e==0,"select t3.office_name,t1.total_spec_gene_rate `今年`,t2.total_spec_gene_rate `去年`,t1.freight_yoy `同比`  from  (select distinct office_code,office_name from freight_sales_amount_rate_analyze_day) t3 left join

(SELECT data_day,office_name,office_code,ifnull(total_spec_gene_rate,0) total_spec_gene_rate,ifnull(freight_yoy,0)-1 as `freight_yoy`  FROM  `freight_sales_amount_rate_analyze_day`  where "+if(len(a)=0,"1=1"," distribution_center_name  = '"+a+"'")+" and  data_day = '"+b+"'  group by office_name,office_code) t1 on t1.office_code=t3.office_code left join

(SELECT data_day,office_name,office_code,ifnull(total_spec_gene_rate,0) total_spec_gene_rate FROM  `freight_sales_amount_rate_analyze_day`  where "+if(len(a)=0,"1=1"," distribution_center_name  = '"+a+"'")+" and  data_day = date_sub('"+b+"' ,interval 1 year) group by office_name,office_code) t2 on t2.office_code=t3.office_code","")}

image.png

  • pengfuqiang96 pengfuqiang96(提问者) 也不对
    2021-06-01 14:30 
  • snrtuemc snrtuemc 回复 pengfuqiang96(提问者) 你仔细检查,你的几个凭借参数,不要加单引号的加,要加的不加,第一个参数b那边就有问题了,还有,if不能在引号里面,sql中没有if函数,if是帆软自己函数,你的语句问题多的很, 可以的话,你把sql贴出来,给你修改后,你在几拷贝在试试
    2021-06-01 14:35 
  • pengfuqiang96 pengfuqiang96(提问者) sql 贴出来了
    2021-06-01 14:42 
  • snrtuemc snrtuemc 回复 pengfuqiang96(提问者) 看下修改答案
    2021-06-01 14:50 
  • pengfuqiang96 pengfuqiang96(提问者) 对了 SQL可以运行了。
    2021-06-01 15:02 
最佳回答
1
Z4u3z1Lv6专家互助
发布于2021-6-1 11:59

看你的逻辑应该是当e=0时才执行语句,还到不如将这个判断放WHERE 后面 IF(e=0,"AND 1=1","and 1<>1")

最佳回答
0
圣体叶小凡Lv6高级互助
发布于2021-6-1 14:31

${

IF(a=1,

"select 

a.id, 

a.name

from table a

where 1=1

"+if(len(a)=0,'','and distribution_center_name like = '+a+'')+"

and data_day ='"+b+"'

","")}

自己拿过去按照这个格式改一改吧

  • pengfuqiang96 pengfuqiang96(提问者) 不得行 ,改完后 带入的参数a 被看作了一个列名。unknown column
    2021-06-01 14:40 
最佳回答
0
shirokoLv6资深互助
发布于2021-6-1 14:55(编辑于 2021-6-1 14:56)

 ${if(e==0,

"

select 

t3.office_name,

t1.total_spec_gene_rate `今年`,

t2.total_spec_gene_rate `去年`,

t1.freight_yoy `同比`

from 

(

select distinct office_code,office_name from freight_sales_amount_rate_analyze_day

) t3 left join

(

SELECT data_day,office_name,office_code,

ifnull(total_spec_gene_rate,0) total_spec_gene_rate,

ifnull(freight_yoy,0)-1 as `freight_yoy`

FROM `freight_sales_amount_rate_analyze_day`  

where 

"+if(len(a)=0,'1=1'," distribution_center_name  = '"+a+"'")+"

and  data_day =   '"+b+"' 

group by office_name,office_code

) t1 on t1.office_code=t3.office_code left join

(

SELECT data_day,office_name,office_code,

ifnull(total_spec_gene_rate,0) total_spec_gene_rate

FROM `freight_sales_amount_rate_analyze_day`  

where 

"+if(len(a)=0,'1=1'," distribution_center_name  = '"+a+"'")+"

and  data_day = date_sub('"+b+"' ,interval 1 year)

group by office_name,office_code

) t2 on t2.office_code=t3.office_code

"

,"")}

跟if怎么用没关系,就是引号没对上,要注意字符串拼接后还需要留下单引号给sql用

  • 4关注人数
  • 1260浏览人数
  • 最后回答于:2021-6-1 14:56
    请选择关闭问题的原因
    确定 取消
    返回顶部