如何获得id的降序

时间:2016-02-11 05:19:31

标签: php mysql

我使用查询 -

以desc顺序从表中获取一些记录
SELECT trn_id,cus_name,mobile,product,product_slno,entry_by FROM 
  insur_trn WHERE trn_id !='' ORDER BY trn_id DESC;

我想让trn_id像:

trn_id
128
127
126
125 

但是在应用ORDER BY DESC时它就像:

+--------+
| 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     |
+--------

如何按降序获取trn_id,这个查询只对第一个数字排序trn_id,但我希望它是128,127,126 ......它不会那样出现

1 个答案:

答案 0 :(得分:1)

看起来您的trn_id是一个字符串,因此排序按字母顺序完成。

如果你想要数字,你应该把它转换为数字:

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;