4

这是下面的查询,我希望点列按 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
4

2 回答 2

2

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

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
于 2013-06-27T12:38:46.763 回答
1

尝试这个:

SELECT * FROM  bonus ORDER BY CAST(points AS INT) ASC;
于 2013-06-27T12:35:40.520 回答