简体   繁体   English

MySQL ORDER BY,每种情况基于不同字段的CASE

[英]MySQL ORDER BY, CASE Based on Different Fields for Each Case

The query below works great. 下面的查询效果很好。 It orders results in two tiers, both based on s.points . 它根据s.points结果分为两层。

  • Tier 1: All results with s.datesubmitted within the last hour. 第1层:所有带有s.datesubmitted结果s.datesubmitted在过去一小时内s.datesubmitted
  • Tier 2: All results with s.datesubmitted previous to 1 hour ago. 第2层:1小时前s.datesubmitted所有带有s.datesubmitted结果。

The results in tier 1 take precedence over tier 2. So anything submitted within the last hour will display above anything submitted over an hour ago regardless of s.points . 第1层的结果优先于第2层。因此,无论s.points什么,在过去一小时内提交的任何内容都将显示在一小时前提交的所有内容s.points

The results in Tier 2 are all items submitted over 1 hour ago. 方法2中的结果是1小时前提交的所有项目。 Like Tier 1, they are ordered by s.points . 像第1层一样,它们由s.points How could I leave Tier 1 ordered by s.points , but make tier 2 ordered by most_recent ? 我怎样才能让s.points排列第1层,而让most_recent排列第2层?

Thanks in advance, 提前致谢,

John 约翰

   $sqlStr = "SELECT s.loginid, s.title, s.url, s.displayurl, s.points, s.datesubmitted, l.username,
               s.submissionid, s.subcheck, s.topten, COUNT(c.commentid) countComments, 
               GREATEST(s.datesubmitted, COALESCE(MAX(c.datecommented), s.datesubmitted)) AS most_recent
          FROM submission s
          JOIN login l ON s.loginid = l.loginid
     LEFT JOIN comment c ON s.submissionid = c.submissionid
      GROUP BY s.submissionid
      ORDER BY 
        CASE 
                 WHEN s.datesubmitted > DATE_SUB(NOW(), INTERVAL 1 HOUR) THEN 0 
                 WHEN s.datesubmitted > DATE_SUB(NOW(), INTERVAL 1 HOUR) THEN 1
                 ELSE 2 
               END, s.points DESC

         LIMIT $offset, $rowsperpage";
ORDER BY CASE 
             WHEN s.datesubmitted >= DATE_SUB(NOW(), INTERVAL 1 HOUR) THEN CONCAT(1, ',', s.points)
             WHEN s.datesubmitted < DATE_SUB(NOW(), INTERVAL 1 HOUR) THEN CONCAT(0, ',', s.datesubmitted)
         END DESC

In this case: 在这种情况下:

  • Tier 1: All records within an hour will be sorted by points DESC 方法1:一个小时内的所有记录将按points DESC排序
  • Tier 2: All other records are sorted by datesubmitted DESC (most recent) 第2层:所有其他记录均按datesubmitted DESC (最新)排序
SELECT
  loginid, title, url, displayurl, points, datesubmitted, username,
  submissionid, subcheck, s.topten, countComments, most_recent
FROM (
  SELECT
    s.loginid, s.title, s.url, s.displayurl, s.points, s.datesubmitted, l.username,
    s.submissionid, s.subcheck, s.topten, COUNT(c.commentid) countComments, 
    GREATEST(s.datesubmitted, COALESCE(MAX(c.datecommented), s.datesubmitted)) AS most_recent,
    s.datesubmitted > DATE_SUB(NOW(), INTERVAL 1 HOUR) AS is_within_hour
  FROM submission s
    INNER JOIN login l ON s.loginid = l.loginid
    LEFT JOIN comment c ON s.submissionid = c.submissionid
  GROUP BY s.submissionid
) x
ORDER BY
  is_within_hour DESC,
  CASE WHEN     is_within_hour THEN points        END DESC,
  CASE WHEN NOT is_within_hour THEN datesubmitted END DESC
LIMIT $offset, $rowsperpage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM