以下查询效果很好。但是,我想修改它。
我希望when s.topten = 1 then 0 else 1 end
的排序只能在s.datesubmitted
距现在不到24小时的时候完成。
我该怎么做?
提前致谢,
约翰
$sqlStr = "SELECT s.loginid, s.title, s.url, s.displayurl, 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
INNER JOIN login l ON s.loginid = l.loginid
LEFT OUTER JOIN comment c ON s.submissionid = c.submissionid
GROUP BY s.submissionid
ORDER BY case when s.topten = 1 then 0 else 1 end, most_recent DESC
LIMIT $offset, $rowsperpage";
答案 0 :(得分:0)
添加另一个WHEN
子句,用于检查间隔并生成-1
。
答案 1 :(得分:0)
使用:
SELECT s.loginid, s.title, s.url, s.displayurl, 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 DAY) AND s.topten = 1 THEN 0
WHEN s.datesubmitted > DATE_SUB(NOW(), INTERVAL 1 DAY) AND s.topten != 1 THEN 1
ELSE 2
END, most_recent DESC
LIMIT $offset, $rowsperpage