ahmed galal ahmed galal - 1 year ago 83
SQL Question

Order by varchar stored as number giving wrong output

i'm trying a simple select query but the output is totally wrong , mainly it's a laravel project but i tried the sql on phpmyadmin and the error still the same

enter image description here

any help would be great , thank you.

Answer Source

If the datatype of property_price is VARCHAR

SELECT * FROM properties ORDER BY property_price+0 ASC LIMIT 10;


SELECT * FROM properties ORDER BY CAST(property_price AS UNSIGNED) ASC LIMIT 10;


Change the datatype of property_price column to a numeric type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download