MySQL中的IF语句输出

时间:2015-11-02 02:37:02

标签: mysql

我有这个查询输出:

enter image description here

“LesserValue”列因为if语句而存在:

if((@RightD < @LeftD), 'RightD', 'LeftD') as LesserValue

似乎没有向我显示正确的输出(@RightD是RightD列,@ LeftD是LeftD列)。

对于RightD和LeftD,查询为:

@RightD := format(replace(@RDeduc,',','') - @Base, 2) as RightD,
@LeftD := format(@Base - replace(@LDeduc,',',''), 2) as LeftD,

这是我的完整测试查询:

select
distinct a.employee_number as EmployeeNumber,
employee_name as EmployeeName,
regular_days as GrossPay,
@TotalDeductions := ((lates + undertime) + absences) as TotalDeductions,
@Base := replace(`regular_days`,',','') - @TotalDeductions as Base,
@RDeduc := replace(format((select deduction from tbmonthlytax where deduction > @Base and status = employee_number_dependents order by deduction limit 1), 2),',','') as RDeduc,
@LDeduc := replace(format((select deduction from tbmonthlytax where deduction < @Base and status = employee_number_dependents order by deduction desc limit 1), 2),',','') as LDeduc,
@RightD := replace(format(@RDeduc - @Base, 2),',','') as RightD,
@LeftD := replace(format(@Base - @LDeduc, 2),',','') as LeftD,
if(@RightD < @LeftD, @RightD, @LeftD) as LesserValue

这     tbpayslip a,tbemployee b

,其中     a.employee_number = b.employee_number

1 个答案:

答案 0 :(得分:0)

最后我自己找到了答案。 ABS()解决了我的问题。

if(ABS(@RightD) < ABS(@LeftD), 'Right', 'Left') as LesserValue

我希望这也可以帮助你。

谢谢你们。