我有一个按输入日期排序的列表:
- JOB C,en-GB,2016-01-12 08:00:00
- 作业 B,en-GB,2016-01-12 08:10:00
- 作业 A,en-GB,2016-01-12 08:20:00
假设我现在添加:
- 工作 B,法国-法国,2016-01-12 08:30:00
在当前查询中,列表将如下所示:
- JOB C,en-GB,2016-01-12 08:00:00
- 作业 B,en-GB,2016-01-12 08:10:00
- 作业 A,en-GB,2016-01-12 08:20:00
- 工作 B,法国-法国,2016-01-12 08:30:00
参见http://sqlfiddle.com/#!9/8d61d7/2
我需要的结果如下:
- JOB C,en-GB,2016-01-12 08:00:00
- 作业 B,en-GB,2016-01-12 08:10:00
- 工作 B,法国-法国,2016-01-12 08:30:00
- 作业 A,en-GB,2016-01-12 08:20:00
因此初始排序是按created_at排序,但JOB B, fr-FR现在位于JOB B, en-GB记录下方。
抱歉,我的解释很复杂......
最佳答案
如果是这种情况,我更愿意使用 UNION ALL 并将“id”添加到 select 语句中。 这是代码
SELECT *
FROM (
select 1 as id, name, job, created_at from yourtable where created_at <= '2016-01-12 08:10:00'
union all
select 2 as id, name, job, created_at from yourtable where job = 'fr-FR'
union all
select 3 as id, name, job, created_at from yourtable where created_at = '2016-01-12 08:20:00'
) x
order by x.id, x.created_at
关于MySQL 排序依据 : conditional multiple columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34747675/