对于下面的查询,我想更改结果的排序方式。
目前,结果按ORDER BY most_recent DESC
排序。我想保留这个排序,但我希望s.topten
= 1的任何行在s.topten
= 0的行之上排序。
我该怎么做?
提前致谢,
约翰
$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 most_recent DESC
LIMIT $offset, $rowsperpage";
答案 0 :(得分:1)
您可以在ORDER BY
子句中使用CASE
语句执行此操作:
ORDER BY case when s.topten = 1 then 0 else 1 end,
most_recent DESC