Order given elements with SQL ORDER BY
Given:
SELECT projectID, url开发者_开发技巧ID, 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
I need to order them so that all projects with ID = 111 come LAST in the returned data set.
So for example it might return:
Project ID
---------
100
100
100
156
156
143
122
111
111
111
190
154
87
But I need all the 111's to appear at the END of the list, without using two queries, one to select the 111's and the other to select the rest is not good enough unfortunately, as this is quite a resource intensive query.
ORDER BY
CASE projectID WHEN 111 THEN 1 ELSE 0 END,
projectID /* etc... */
add
order by cast (projectID as integer)
精彩评论