This topic created in 4325 days ago, the information mentioned may be changed or developed.
数据表中有一个字段为 start_time,储存的是Unix时间戳,值包括过去时间和未来时间,要实现的排序原则如下:
1、过去的时间按照时间戳递减排序
2、未来的时间按照未来时间戳减去现在时间戳的值递增排序
例:
现在时间是 1405495903 (2014/7/16 15:31:43)
排序前:
1405495901 1405495902 1405495904 1405495905
排序后:
1405495904 1405495905 1405495902 1405495901
6 replies • 2014-07-17 13:50:50 +08:00
 |
|
1
hellojinjie Jul 16, 2014
select start_time from table where start_time < unix_timestamp() order by start_time desc union select start_time from table where start_time >= unix_timestamp() order by start_time asc
|
 |
|
2
caofugui Jul 16, 2014
首先这个用SQL去操作很蛋疼,程序上用两条SQL语句不就搞定? 其次设计上本身就不合理,过去时间跟未来时间怎么能混在一起呢?
|
 |
|
5
imxz Jul 17, 2014
@ hellojinjie 测试了一下,然后发现 order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。 所以问题还是没有解决。。。
|
 |
|
6
imxz Jul 17, 2014
这样子就行了:
select * from (select * from `table` where `start_time` >= unix_timestamp() order by `start_time` asc)tmpa union select * from (select * from `table` where `start_time` < unix_timestamp() order by `start_time` desc)tmpb
|