對於以下查詢,我訂購了ORDER BY s.points DESC
,它工作正常。但我想改變它,所以它的訂單由s.points
降序而s.datesubmitted
是在最後一小時內。字段s.datesubmitted
是一個時間戳。否則,我想按most_recent
降序排列。根據時間戳字段的年齡對MySQL查詢進行排序
我該怎麼做?
由於提前,
約翰
$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 s.points DESC
LIMIT $offset, $rowsperpage";
您可以在答案的後半部分顯示完整代碼嗎? – John 2011-05-19 23:59:39
'ORDER BY(CASE when s.submitted> DATE_SUB(NOW(),INTERVAL 1 HOUR)then 1 ELSE 0 END)DESC,(CASE when s.submitted> DATE_SUB(NOW(),INTERVAL 1 HOUR) THEN s。點ELSE GREATEST(s.datesubmitted, COALESCE(MAX(c.datecommented),s.datesubmitted))END)DESC' – 2011-05-20 00:25:15
謝謝...但是這並沒有返回任何結果。只是一個空白頁面。 – John 2011-05-20 00:33:25