sql 按时间排序

dhywjd 2011-09-16 08:05:40
order by Lastreply desc
Lastreply 就是下面所代表的时间
2011-9-8 12:34:08
2011-9-7 12:12:50
2011-9-16 17:35:41
我疑问的就是在同一天按时间逆序排序是正常的,可是就像第三条数据不跟前两条数据在同一天这排序就出问题了,照理说第三条数据应该在最上面的,为什么sql按时间排序会出现这问题
...全文
172 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
koumingjie 2011-09-16
  • 打赏
  • 举报
回复
字符串排序跟日期排序不一样,楼主要注意了
-晴天 2011-09-16
  • 打赏
  • 举报
回复
如果你的 Lastreply 不是字符型,则:
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
-晴天 2011-09-16
  • 打赏
  • 举报
回复
不会吧!
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

你是不是其他什么地方弄错了.
dhywjd 2011-09-16
  • 打赏
  • 举报
回复
那该怎么改,求完整sql语句
geniuswjt 2011-09-16
  • 打赏
  • 举报
回复
妹子好活跃[Quote=引用 3 楼 bitls 的回复:]
引用 2 楼 geniuswjt 的回复:
SQL code

估计你的Lastreply是字符型的,所以导致
2011-9-16 17:35:41 在 2011-9-7 12:12:50 下面
因为判断到第7位时,1<7


由意楼上观点
[/Quote]
bitls 2011-09-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 geniuswjt 的回复:]
SQL code

估计你的Lastreply是字符型的,所以导致
2011-9-16 17:35:41 在 2011-9-7 12:12:50 下面
因为判断到第7位时,1<7
[/Quote]

由意楼上观点
geniuswjt 2011-09-16
  • 打赏
  • 举报
回复

估计你的Lastreply是字符型的,所以导致
2011-9-16 17:35:41 在 2011-9-7 12:12:50 下面
因为判断到第7位时,1<7
geniuswjt 2011-09-16
  • 打赏
  • 举报
回复

order by convert(datetime,Lastreply) desc

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧