Riyad Malakh
Riyad Malakh

Reputation: 3

Wrong OrderBy in Sql query ordered in opposite way

I tried to order by Integer but it seemed that it order by the larger to the smaller number but it order by 2-digit then 3-digit
sc is the score i need to order
Code i used

SELECT * FROM `users`  ORDER BY `sc`  DESC

The result are :
47
3
102

Upvotes: 0

Views: 67

Answers (3)

Jens
Jens

Reputation: 69440

Looks like datetype of sc is char/varchar. You have to cast it to an integer:

SELECT * FROM `users`  ORDER BY cast(`sc` as UNSIGNED) DESC  

Upvotes: 3

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You may use silent conversion to integer as

select * from users order by sc+0 desc

Upvotes: 0

Erick Cortorreal
Erick Cortorreal

Reputation: 389

Your query should be like this:

SELECT * FROM `users`  ORDER BY CAST(`sc` AS SIGNED INTEGER) DESC

Upvotes: 1

Related Questions