MySQL订购一个日期范围到一开始,然后按其他条件订购一切 [英] MySQL ordering a date range to the beginning, then ordering everything else by other criteria

查看:138
本文介绍了MySQL订购一个日期范围到一开始,然后按其他条件订购一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有创建日期的项目表,我想找出一种方式来订购它们,以便在过去两周中创建的项目首先显示,而不是在最后两个星期之后出现,按字母顺序排列。

I have a table of items that have a "Date Created" field, and I'm trying to figure out a way to order them so that items created in the past two weeks appear first, and items that were not created in the last two weeks appear after that, sorted alphabetically.

我知道如何使用多个sql查询,但是如果可能,我不想这样做。是否可以这样做?

I know how to do it with multiple sql queries, but I'd prefer not to do that if possible. Is it possible to do this?

推荐答案

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

更新的答案

UPDATED ANSWER

(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)

LIMIT 当您必须同时应用asc和desc排序时,必须使用它。

LIMIT's use is necessary when you have to apply both asc and desc sorting within union.

这篇关于MySQL订购一个日期范围到一开始,然后按其他条件订购一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆