Sami El Hilali
Sami El Hilali

Reputation: 1031

Mysql/PHP How to Select and Order result in a float format?

Please I have a field value (type text) that contains some values (text and also some int or float values).

So I want to select only the float and integers values and order theme by the field value.

For the moment I can get floats separately but the problem is that in result I get theme ordered in a text format and not in float or integer format (and it's normal I guess). Example :

33
33358
80
88000
90
99

Please masters, How could I tell my query to consider results as floats and not text and get instead this result :

33
80
90
99
33358
88000

Thanks in advance

Upvotes: 1

Views: 6225

Answers (4)

Sithu
Sithu

Reputation: 4862

Storing mixed values in a field with type text is not a good idea. If you want to make sorting and filtering on the field, I would recommend to use them as separated fields - text, int, float, etc.
If you go deep as database administrator, it will have negative performance.

Upvotes: 1

Bhavik Shah
Bhavik Shah

Reputation: 2291

cast VALUE to float/int format when you select that variable in your query and then order the result set by that column.

eg./ SELECT CAST(value as decimal(10,5)) AS customname FROM tablename order by customname

remember, you can cast to whatever you want.

Upvotes: 0

Mitch Satchwell
Mitch Satchwell

Reputation: 4830

ORDER BY field * 1

Or

ORDER BY CAST(field as DECIMAL(10,5))

Both should do the same thing but the first is easier/quicker to write.

Upvotes: 14

user7282
user7282

Reputation: 5196

use like the code below

ORDER BY ABS(columnname)

Upvotes: 2

Related Questions