首页 > 开发 > MySQL > 正文

MySQL数据排序

2017-09-08 09:12:04  来源:网友分享
SELECT c.WORK_DATE,
       c.ONDUTY_TIME,
       c.OFFDUTY_TIME,
       group_concat(d.start_date) startDate,
       group_concat(d.end_date) endDate,
       group_concat(d.duty_explain) dutyExplain,
       group_concat(d.duty_status_id) dutyStatusId
  FROM    (SELECT a.WORK_DATE,
                  a.ONDUTY_TIME,
                  a.OFFDUTY_TIME,
                  b.DUTY_APPLY_ID
             FROM    (SELECT ONDUTY_TIME,
                             OFFDUTY_TIME,
                             WORK_DATE,
                             SEQ_ID
                        FROM tbl_duty
                       WHERE     USER_ID = 'xiaodong'
                             AND WORK_DATE >= '2012/3/20 星期二 0:00:00'
                             AND WORK_DATE <= '2012/3/29 星期二 0:00:00') a
                  LEFT JOIN
                     tbl_duty_to_apply b
                  ON a.seq_id = b.DUTY_ID) c
       LEFT JOIN
          tbl_duty_apply d
       ON c.DUTY_APPLY_ID = d.SEQ_ID
GROUP BY c.WORK_DATE;

这段sql 我想按照c.WORK_DATE倒叙排列,我要往哪加排序的sql语句呢 自己加了总是出错,主要是SQL是别人帮写的,现在急着要加排序。。。感谢!

解决方案

理论上,家在 GROUP BY 之后

SELECT c.WORK_DATE,
       c.ONDUTY_TIME,
       c.OFFDUTY_TIME,
       group_concat(d.start_date) startDate,
       group_concat(d.end_date) endDate,
       group_concat(d.duty_explain) dutyExplain,
       group_concat(d.duty_status_id) dutyStatusId
  FROM    (SELECT a.WORK_DATE,
                  a.ONDUTY_TIME,
                  a.OFFDUTY_TIME,
                  b.DUTY_APPLY_ID
             FROM    (SELECT ONDUTY_TIME,
                             OFFDUTY_TIME,
                             WORK_DATE,
                             SEQ_ID
                        FROM tbl_duty
                       WHERE     USER_ID = 'xiaodong'
                             AND WORK_DATE >= '2012/3/20 星期二 0:00:00'
                             AND WORK_DATE <= '2012/3/29 星期二 0:00:00') a
                  LEFT JOIN
                     tbl_duty_to_apply b
                  ON a.seq_id = b.DUTY_ID) c
       LEFT JOIN
          tbl_duty_apply d
       ON c.DUTY_APPLY_ID = d.SEQ_ID
GROUP BY c.WORK_DATE
ORDER BY c.WORK_DATE DESC;