MySQL将日期范围排序到开头,然后按其他条件排序其他所有内容

时间:2011-09-04 19:19:55

标签: php mysql date select

我有一个项目表,其中包含“创建日期”字段,我正在尝试找出一种方法来对它们进行排序,以便过去两周内创建的项目首先出现,而未创建的项目则出现在最后两周出现在那之后,按字母顺序排序。

我知道如何使用多个sql查询,但如果可能的话,我宁愿不这样做。有可能这样做吗?

1 个答案:

答案 0 :(得分:3)

select * from table
order by
case when date_created > curdate() - interval 2 week then 1 else 2 end,item

更新的答案

(select * from table
where date_created > curdate() - interval 2 week 
order by date_created desc limit 0,10000000000)
union all
(select * from table
where date_created < curdate() - interval 2 week 
order by item
limit 0,10000000000)
当你必须在union中同时应用asc和desc排序时,必须使用

LIMIT