根据变量是等于1还是0来更改查询顺序

时间:2010-10-13 18:51:03

标签: php mysql

对于下面的查询,我想更改结果的排序方式。

目前,结果按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";   

1 个答案:

答案 0 :(得分:1)

您可以在ORDER BY子句中使用CASE语句执行此操作:

ORDER BY case when s.topten = 1 then 0 else 1 end, 
 most_recent DESC