多个mysql ORDER BY用于多维排序/分组 [英] Multiple mysql ORDER BY's for multidimensional ordering/grouping

查看:196
本文介绍了多个mysql ORDER BY用于多维排序/分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据需要同时订购多种方式。



简化后,表格可以概括为:



任务




  • id

  • >
  • 日期(日期)

  • 完成(0/1)



我需要产生这些任务的列表,主要由完成排序,其次由日期排序,然后通过组分组。



这个组是什么导致我的问题,我有一个感觉这可能更好地实现与PHP,因为它可能不可能作为查询的一部分 - 最好的方法的提示,欢迎,它是当然不是纯粹的mysql。



目前我使用的是:

 code> ORDER BY complete,group,date 

所有不完全(0)任务在顶部,完整(1)任务在底部;



但是,一个组可能会在底部结束,日期排序在较宽的上下文中不正确 - 组本身没有被放置给它的任务日期。下面是一个不需要的输出(有序)的例子:

 任务#2  - 组(假),完成(2013-11-01)
任务#4 - 组(false),完​​成(0),日期(2013-12-01)
任务#5 - ,日期(2013-10-01)
任务#3 - 组(1),完成(0),日期(2013-12-01)
任务#1 - 1),date(2013-11-01)

正如您所看到的,日期排序不正确分组的项目,其中排序发生在 组内。任务#5位于第三位,即使它有最早的日期。



我想看到的输出如下:

 任务#5  - 组(1),完成(0),日期(2013-10-01)
任务# ,完成(0),日期(2013-12-01)
任务#2 - 组(假),完成(0),日期(2013-11-01)
任务# false),完​​成(0),日期(2013-12-01)
任务#1 - 组(false),完​​成(1),日期(2013-11-01)

如果组中的任务的日期早于单个任务,则应该首先对整个组进行排序(假设对于一个组,您只能看到顶部任务,因此任务#3将被折叠,可视化)。

我已经尝试改变'ORDER BY'子句的顺序,但似乎没有任何组合实现我的后继 - 总是最终在错误的地方。



任何帮助非常感激。

解决方案

例如:

  SELECT t1.ID,t1.`Group`,t1.Complete,t1.Due 
FROM任务t1
LEFT JOIN(
SELECT Complete,
t.`Group`,
MIN(Due)AS MinDate
FROM task t
GROUP BY完成,t.`Group`)t2 ON t1.complete = t2.complete AND t1.`Group` = t2.`Group`
ORDER BY t1.complete,IFNULL(t2.MinDate,t1.Due),` Group`,t1.Due

对于每个组记录,将其加入该组中最早的记录,那么您可以按照最早的组日期(如果未分组,则只是日期)进行排序。



SQLFiddle


I have a set of data that needs to be ordered in multiple ways simultaneously.

Simplified, the table can be summarised as:

Task

  • id
  • group (int)
  • date (date)
  • complete (0/1)

I need to produce a list of these tasks, sorted primarily by 'complete', secondarily by 'date' and then grouped by 'group'.

The group is what's causing the issue for me and I have a feeling this might be better achieved with PHP, as it may not be possible as part of the query - tips on the best way to approach this are welcome, it certainly doesn't have to be pure mysql.

Currently I'm using:

ORDER BY complete, group, date

This works perfectly for 'ungrouped' tasks, all the not complete (0) tasks are at the top, with the complete (1) tasks at the bottom; with each set of complete/not complete tasks sorted by date.

However a group can end up bunched at the bottom, with the date ordering incorrect in the wider context - the group itself isn't placed given its tasks dates. Here's an example of the unwanted output (ordered):

Task #2 - Group(false), Complete(0), date(2013-11-01)
Task #4 - Group(false), Complete(0), date(2013-12-01)
Task #5 - Group(1), Complete(0), date(2013-10-01)
Task #3 - Group(1), Complete(0), date(2013-12-01)
Task #1 - Group(false), Complete(1), date(2013-11-01)

As you can see the date ordering is incorrect for the grouped items, with the ordering taking place within the group. Task #5 is placed third, even though it has the earliest date.

The output I'd like to see is as follows:

Task #5 - Group(1), Complete(0), date(2013-10-01)
Task #3 - Group(1), Complete(0), date(2013-12-01)
Task #2 - Group(false), Complete(0), date(2013-11-01)
Task #4 - Group(false), Complete(0), date(2013-12-01)
Task #1 - Group(false), Complete(1), date(2013-11-01)

i.e. if a task within a group has a date earlier than an individual task, the whole group should be ordered first (imagine that with a group you only see the 'top' task, so task #3 would be collapsed, visually).

I've tried changing the order of the 'ORDER BY' clause, but it doesn't seem that any combination achieves what I'm after - something always ends up in the wrong place.

Any help greatly appreciated.

解决方案

How about something like:

SELECT t1.ID, t1.`Group`, t1.Complete, t1.Due 
FROM task t1
LEFT JOIN (
    SELECT Complete, 
        t.`Group`, 
        MIN(Due) AS MinDate 
    FROM task t
    GROUP BY Complete, t.`Group` ) t2 ON t1.complete = t2.complete AND t1.`Group`  = t2.`Group`     
ORDER BY t1.complete, IFNULL(t2.MinDate, t1.Due), `Group`, t1.Due 

For each Group record, join it to the earliest record from that group, then you can order by the earliest group date (and if not grouped, then just the date).

SQLFiddle

这篇关于多个mysql ORDER BY用于多维排序/分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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