set @fuel_type='hsd';
select
ro.ro_id,
(case
when if(@fuel_type='ms',r.ms_field_input_id,r.hsd_field_input_id)= 0
then if(@fuel_type='ms',concat(r.remark_ms,r.remark_branded),r.remark_hsd)
else
f.field_name
end )
from
retail_outlets ro left join
volume_trackers r on ro.ro_id=r.ro_id,
field_inputs_master f
where
if(@fuel_type='ms',r.ms_field_input_id = f.field_id,r.hsd_field_input_id=f.field_id)
and ro.pricing_module=1 and if(@fuel_type='ms',ro.ms,ro.hsd)=1 and r.date_inserted='2017-02-06'
;
我需要在retail_outlets表中显示所有ro_id,但只显示与卷跟踪器表匹配的ro_id
答案 0 :(得分:0)
不是积极的,但我认为这就是你所追求的。
更改如下:
假设r.date_inserted是varchar2数据类型或' 2017-02-06'隐式正确地投射。
SET @fuel_type='hsd';
SELECT
ro.ro_id ,
CASE
WHEN IF(@fuel_type='ms',r.ms_field_input_id,r.hsd_field_input_id)= 0
THEN IF(@fuel_type='ms',concat(r.remark_ms,r.remark_branded),r.remark_hsd)
ELSE f.field_name
END
FROM retail_outlets ro
LEFT JOIN volume_trackers r
ON ro.ro_id = r.ro_id
AND r.date_inserted = '2017-02-06'
LEFT JOIN field_inputs_master f
ON f.field_id = CASE WHEN @fuel_type='ms'
THEN r.ms_field_input_id
ELSE r.hsd_field_input_id
END
WHERE ro.pricing_module=1
AND IF(@fuel_type ='ms',ro.ms,ro.hsd)=1;