假设:
SELECT projectID, urlID, COUNT(1) AS totalClicks, projectPage,
(SELECT COUNT(1)
FROM tblStatSessionRoutes, tblStatSessions
WHERE tblStatSessionRoutes.statSessionID = tblStatSessions.ID
AND tblStatSessions.projectID = tblAdClicks.projectID
AND (tblStatSessionRoutes.leftPageID = tblAdClicks.projectPage OR
tblStatSessionRoutes.rightPageID = tblAdClicks.projectPage)) AS totalViews
FROM tblAdClicks
WHERE projectID IN (SELECT projectID FROM tblProjects WHERE userID = 5)
GROUP BY projectID, urlID, projectPage
我需要对它们进行排序,以便ID = 111的所有项目在返回的数据集中都是最后的。
例如,它可能会返回:
Project ID
---------
100
100
100
156
156
143
122
111
111
111
190
154
87
但我需要所有的111出现在列表的END,不使用两个查询,一个选择111,另一个选择其余的不幸不幸,因为这是一个资源密集型查询。
答案 0 :(得分:5)
ORDER BY
CASE projectID WHEN 111 THEN 1 ELSE 0 END,
projectID /* etc... */
答案 1 :(得分:0)
添加
order by cast (projectID as integer)