Reputation: 129
i am fetching some records from a table in desc order with query-
SELECT trn_id,cus_name,mobile,product,product_slno,entry_by FROM
insur_trn WHERE trn_id !='' ORDER BY trn_id DESC;
i wanted trn_id to come like:
trn_id
128
127
126
125
but while applying ORDER BY DESC it coming like:
+--------+
| trn_id |
+--------+
| 93 |
| 92 |
| 91 |
| 90 |
| 9 |
| 89 |
| 88 |
| 87 |
| 86 |
| 85 |
| 84 |
| 83 |
| 82 |
| 81 |
| 80 |
| 8 |
| 79 |
| 78 |
| 77 |
| 76 |
| 75 |
| 74 |
| 73 |
| 72 |
| 71 |
| 70 |
| 7 |
| 69 |
| 68 |
| 65 |
| 64 |
| 63 |
| 62 |
| 61 |
| 60 |
| 6 |
| 59 |
| 58 |
| 5 |
| 4 |
| 39 |
| 38 |
| 37 |
| 35 |
| 34 |
| 33 |
| 32 |
| 31 |
| 30 |
| 3 |
| 29 |
| 28 |
| 27 |
| 26 |
| 25 |
| 24 |
| 23 |
| 22 |
| 20 |
| 2 |
| 19 |
| 18 |
| 17 |
| 16 |
| 15 |
| 14 |
| 13 |
| 128 |
| 127 |
| 126 |
| 125 |
| 124 |
| 123 |
| 122 |
| 121 |
| 120 |
| 12 |
| 119 |
| 118 |
| 117 |
| 116 |
| 115 |
| 114 |
| 113 |
| 112 |
| 111 |
| 110 |
| 11 |
| 109 |
| 108 |
| 107 |
| 106 |
| 105 |
| 10 |
+--------
how to get trn_id in descending order, this query sorting the trn_id by first digit only, but i want it by 128,127, 126.... its not coming like that way
Upvotes: 1
Views: 562
Reputation: 12953
looks like your trn_id
is a string, so the sorting is done alphabetically.
if you want it numeric, you should cast it to a number:
SELECT trn_id,cus_name,mobile,product,product_slno,entry_by
FROM insur_trn WHERE trn_id !=''
ORDER BY CAST(trn_id as SIGNED INTEGER) DESC;
Upvotes: 2