[英]Sort DB2 SQL results by WHERE CLAUSE
Is there a way to add a sort order to where clauses? 有没有一种方法可以向where子句添加排序顺序?
For example 例如
SELECT *
FROM media
WHERE media_title LIKE 'query'
OR media_title LIKE 'query string'
OR media_title LIKE 'query string to search for'
I would like to search by relevancy of what the end-user input into a search box, with the last where clause being the most relevant. 我想按最终用户在搜索框中输入的内容的相关性进行搜索,最后一个where子句最相关。
SELECT *
FROM media
WHERE media_title LIKE 'query'
OR media_title LIKE 'query string'
OR media_title LIKE 'query string to search for'
ORDER BY case
when media_title LIKE 'query' then 1
when media_title LIKE 'query string' then 2
when media_title LIKE 'query string to search for' then 3
end
I think this version could be faster and even more readable: 我认为此版本可能会更快,更易读:
SELECT *
FROM media
WHERE media_title IN ('query',
'query string',
'query string to search for')
ORDER BY case
when media_title = 'query' then 1
when media_title = 'query string' then 2
when media_title = 'query string to search for' then 3
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.