数字列中的Asc顺序

时间:2013-06-27 12:32:36

标签: mysql sql select sql-order-by

以下是下面的查询,我希望点列按ASC顺序排列,但是当我执行以下查询时,它不会相应地进行ASC。请参阅下面的结果

SELECT * 
FROM  `bonus` ORDER BY  `bonus`.`points` ASC 

它返回以下数据:

id  referrer_id    points       created             modified
1   10             100      2013-06-01 00:00:00     2013-06-18 00:00:00
2   30             15       2013-06-01 00:00:00     2013-06-18 00:00:00
3   20             45       2013-06-01 00:00:00     2013-06-18 00:00:00

但是我希望点列到ASC这样

id  referrer_id     points      created             modified
1           10          100     2013-06-01 00:00:00     2013-06-18 00:00:00
2           20          45      2013-06-01 00:00:00     2013-06-18 00:00:00
3           30          15      2013-06-01 00:00:00     2013-06-18 00:00:00

2 个答案:

答案 0 :(得分:2)

我认为points列是varchar数据类型,所以你需要手动转换成整数,你会得到答案

SELECT * FROM  `bonus` ORDER BY  cast(bonus.points As int) asc

Create Table #Temp1(Id varchar(10))

insert into #Temp1 values(1)
insert into #Temp1 values(10)
insert into #Temp1 values(2)

Select Top 10 * from #Temp1 Order by Id 

1
10
2

Select Top 10 * from #Temp1 Order by cast(Id As int)

1
2
10
Drop Table #Temp1

答案 1 :(得分:1)

试试这个:

SELECT * FROM  bonus ORDER BY CAST(points AS INT) ASC;