码迷,mamicode.com
首页 > 其他好文 > 详细

Make exact match show at top

时间:2015-03-11 16:50:46      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

SELECT * 
FROM client
WHERE identifyingnumber LIKE ‘%86%‘
ORDER BY LEN(identifyingnumber)


SELECT *
FROM
(
SELECT *, 1 AS PRIO FROM client
WHERE identifyingnumber = ‘86‘
UNION
SELECT *, 2 AS PRIO FROM client
WHERE identifyingnumber LIKE ‘%86%‘
AND identifyingnumber <>‘86‘
) AS X
ORDER BY PRIO



SELECT *
FROM client
WHERE identifyingnumber LIKE ‘%86%‘
ORDER BY 
    CASE WHEN identifyingnumber = ‘86‘ THEN 1 
        WHEN identifyingnumber LIKE ‘86%‘ THEN 2 ELSE 3 END


SELECT *, 
    CASE 
        WHEN identifyingnumber = ‘86‘ THEN 1
        WHEN identifyingnumber LIKE ‘86%‘ THEN 2
        ELSE 3
    END AS Rank
FROM client
WHERE identifyingnumber LIKE ‘%86%‘
ORDER BY Rank



ORDER BY identifyingnumber = ‘86‘ DESC

Make exact match show at top

标签:

原文地址:http://www.cnblogs.com/likem/p/4329983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!