I create a stored procedure in which I pass the "order" dynamically as follows:
CREATE PROCEDURE `getStuff`(IN orderSQL VARCHAR(100)) BEGIN SELECT id, name, createdate FROM mytable ORDER BY CASE WHEN orderSQL='id_desc' THEN CONCAT(id, '') END DESC, CASE WHEN orderSQL='id_asc' THEN CONCAT(id, '') END ASC, CASE WHEN orderSQL='name_desc' THEN name END DESC, CASE WHEN orderSQL='name_asc' THEN name END ASC, CASE WHEN orderSQL='date_desc' THEN CONCAT(createdate, '') END DESC, CASE WHEN orderSQL='date_asc' THEN CONCAT(createdate, '') END ASC END
As you can see, I convert all non-VARCHAR fields to VARCHAR using CONCAT, because mixing the possible order types does not work as described here http://www.4guysfromrolla.com/webtech/010704-1.shtml .
My problem is that now the order by name works, but not string orders are returned as 1,10,11,2,3,4,5,6,7,8,9. etc.
Is there a way to use the dynamic order of a mixed data type and still return in the correct order for int, datetimes, etc.
source share