order by Lastreply desc
Lastreply 就是下面所代表的时间
2011-9-8 12:34:08
2011-9-7 12:12:50
2011-9-16 17:35:41
我疑问的就是在同一天按时间逆序排序是正常的,可是就像第三条数据不跟前两条数据在同一天这排序就出问题了,照理说第三条数据应该在最上面的,为什么sql按时间排序会出现这问题
Lastreply 就是下面所代表的时间
2011-9-8 12:34:08
2011-9-7 12:12:50
2011-9-16 17:35:41
我疑问的就是在同一天按时间逆序排序是正常的,可是就像第三条数据不跟前两条数据在同一天这排序就出问题了,照理说第三条数据应该在最上面的,为什么sql按时间排序会出现这问题
order by convert(datetime,Lastreply) desc
估计你的Lastreply是字符型的,所以导致
2011-9-16 17:35:41 在 2011-9-7 12:12:50 下面
因为判断到第7位时,1<7
create table tb(Lastreply datetime)
insert into tb select '2011-9-8 12:34:08'
insert into tb select '2011-9-7 12:12:50'
insert into tb select '2011-9-16 17:35:41'
go
select * from tb order by Lastreply desc
/*
Lastreply
-----------------------
2011-09-16 17:35:41.000
2011-09-08 12:34:08.000
2011-09-07 12:12:50.000(3 行受影响)*/
go
drop table tb你是不是其他什么地方弄错了.
create table tb(Lastreply varchar(20))
insert into tb select '2011-9-8 12:34:08'
insert into tb select '2011-9-7 12:12:50'
insert into tb select '2011-9-16 17:35:41'
go
select * from tb order by convert(datetime,Lastreply) desc
/*
Lastreply
--------------------
2011-9-16 17:35:41
2011-9-8 12:34:08
2011-9-7 12:12:50(3 行受影响)*/
go
drop table tb