Reputation: 23
I am facing many problems in mysql query. I am writing a query for order by it is showing me result but not in proper order. my table is
+----+--------+--------+
| ID | NAME | SALARY |
+----+--------+--------+
| 1 | hari | 2000 |
| 2 | haria | 2000 |
| 3 | ramesh | 2000 |
| 4 | rajesh | 5000 |
| 5 | john | 21000 |
| 6 | naag | 45000 |
| 7 | sheena | 22000 |
| 8 | anu | 34000 |
| 9 | aman | 15000 |
| 10 | salil | 9000 |
+----+--------+--------+
i want to fetch all salary by order by keyword
mysql> select salary from salary order by salary desc;
+--------+
| salary |
+--------+
| 9000 |
| 5000 |
| 45000 |
| 34000 |
| 22000 |
| 21000 |
| 2000 |
| 2000 |
| 2000 |
| 15000 |
+--------+
mysql> select salary from salary order by salary;
+--------+
| salary |
+--------+
| 15000 |
| 2000 |
| 2000 |
| 2000 |
| 21000 |
| 22000 |
| 34000 |
| 45000 |
| 5000 |
| 9000 |
+--------+
these results are not showing properly .why order by asc and desc is not working . Thanks in advance.
Upvotes: 1
Views: 79
Reputation: 7441
That's because you have text
(or varchar
) input field type. You can see order is by first character and is valid.
Set salary
field type to int and will work as you want:
ALTER TABLE salary MODIFY salary INT NOT NULL;
Upvotes: 0
Reputation: 14389
Most probable reason is that SALARY
field is of type varchar
instead of int
.
The ordering displayed is if salary is of type varchar
.
If you made salary an int
you will have desired ordering.
Upvotes: 1