My issue was a missing comma. Feel free to delete this post while I smack my head into the desk
I have a database that has several VARCHAR columns that are actually integers, yet for certain reasons the columns need to be VARCHAR and not INT. Obviously I’m having sorting issues as the numbers
7
8
9
10
Sort as
10
7
8
9
My SELECT statement uses multiple joins and SELECT variables:
SELECT master.column1, table2.column1, table2.column2, table3.column1 FROM master
LEFT JOIN table2
ON master.column1 = table2.column1
WHERE master.column1 LIKE 'whatever'
OR WHERE master.column2 LIKE 'whatever'
ORDER BY master.column1, master.column2, master.column3....
Is there a way to insert the CAST function on just one of the variables?
i.e SELECT master.column1, CAST(table2.column1 AS UNSIGNED), table2.column2....
I haven’t had success trying it as above, and the MySql manual does not give an example that has multiple SELECT or ORDER BY variables.
Thank You
You probably want to use CAST in the ORDER BY clause.
Select … ORDER BY master.column1, cast(master.column2 as varchar), master.column3….
Using CAST in the Select Clause only affects output not ordering.