mardi 28 avril 2015

Is this a MySQL bug? How to do "if sub-statement" for round function?

if when sub-statements doesn't seem to work for MySQL's round function, e.g.:

/* setup tables: */ create table t(i int); insert t select 0;

select round(1.234, if(i=0,1,2)) from t;

The above query should give either 1.2 or 1.23 but it consistently returns 1.234.

It's the same with case:

select round(1.234, case when i=0 then 1 else 2 end) from t;
select round(1.234, case i when 0 then 1 else 2 end) from t;

How can we get if when sub-statements to work for MySQL? Is this a bug?

(Tested on version 5.5.10 and latest 5.6.24.)

Aucun commentaire:

Enregistrer un commentaire