I have a DB with lots of property information in it and I need to return all the properties and order them by price. However, I at the moment my query is sorted incorrectly:
143555
145666
40777
30888
They should be more like:
30888
40777
143555
145666
How would I go about this? My existing query is as follows:
Code:
SELECT * FROM <table> WHERE location = '<location>' AND <table>.availiable = 'yes'
OR property_type = '<property_type>' AND <table>.availiable = 'yes'
ORDER BY price ASC
You simply add "+ 0" (that's a zero) between the sort field and ASC, so my query becomes:
Code:
SELECT * FROM <table> WHERE location = '<location>' AND <table>.availiable = 'yes'
OR property_type = '<property_type>' AND <table>.availiable = 'yes'
ORDER BY price + 0 ASC
i've had a similiar problem with a property database. the VERY CRUMMY database admin who set it up set EVERY field to CHAR(255) including the price field. well that didn't sort correctly obviously, so in my sql statement i just converted the price field
SELECT * FROM PROPS ORDER BY CONVERT(INT,PRICEFLD) ASC
@TiGGi: Ah, I see your point. It is a VARCHAR(20), you (I assume are thinking it would be better as a FLOAT -- and so am I now)... The field cannot be an INT as there might be ******.99 (or something) going into it (even though there isn't at the mo.) -- I know if I changed it, a property would appear at ******.99 (i'm not very lucky)
@chazzy: So does that mean my "+ 0" has told MySQL to see the data as zero length (i.e. ignore the value's length)?
@silverbullet24: Lol, I didn't even know an SQL query could do that; I assumed that would involve PHP gathering all the info into a massive array and than sorting that first (not something I wanted to deal with -- I'm still very much a newbie )
Bookmarks