I am having a dynamic MySQL code that does have an ORDER BY clause. The table contains multiple columns but the ones that are important are stat and quality.
The stat column is mathematical calculation in select statement with type of INT type while the quality column is of STRING type.
Now I do have a statement of such:
SELECT (col1+col2+col3) as cast(STAT as unsigned), QUALITY FROM GEAR
ORDER BY CASE WHEN 1=1 THEN stat
WHEN 1=0 THEN quality END DESC
Now I do expect the code in above scenario to sort the results by the stat column in integer way:
stat quality
1 normal
2 normal
3 better
4 better
However the actual way the results are displayed is as if the integer was autocasted to string:
stat quality
1 normal
11 normal
12 better
2 better
Now if I:
- Remove the second case 1=0 statement (which is never true)
- Cast the "quality" column to int (from string)
- Change the "quality" value inside WHEN 1=0 THEN QUALITY END DESC to any other numeric column
The statement will work correctly and sort the output by STAT in the integer way.
I am trying to understand why is MySQL (MariaDB) deciding to sort the stat integer type column in a string way if the CASE/IF statement inside ORDER BY contains string type column in the other WHEN and find a way for this to not happen.
Maria
1=0
in awhere
clause), although I doubt the developers will regard it a very important one. – Solarflare Sep 21 '17 at 14:56...when @order_by_setting = 1 then...
)?) – Solarflare Sep 21 '17 at 15:07