sasi
sasi

Reputation: 51

how to sort numerical data in sql table using php

I have data which has 5000 rows with 40 columns. I want to sort the data according to the first column in the row, which contains the id value of the particular row. The values in the id column are like 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,.....100,101,102.....1000,1001..... when I use a SQL statement with the order clause ORDER by id ASC.

Data is sorting in this manner..1,10,100,1000,1001,1002,1003,1004,1005,1006,1007,1008,1009,101.102,103,........

But I want the data to be in ascending order like: 1,2,3,4,5,6,7,8,9,10,11,12,13.....

This is the statement: $result = mysql_query("SELECT * FROM masterdb ORDER BY id ASC");

Upvotes: 1

Views: 693

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

You may want to consider making your id column a numeric type if you expect to have the need to often sort numerically. That being said, one workaround would be to cast the id column to a numeric type and then sort using this:

SELECT *
FROM masterdb
ORDER BY CAST(id AS UNSIGNED)

Upvotes: 2

Related Questions