[英]How to do an exact match followed by ORDER BY in PostgreSQL
I'm trying to write a query that puts some results (in my case a single result) at the top, and then sorts the rest. 我正在尝试编写一个查询,该查询将某些结果(在我的情况下为单个结果)放在顶部,然后对其余结果进行排序。 I have yet to find a PostgreSQL solution.
我尚未找到PostgreSQL解决方案。
Say I have a table called airports
like so. 假设我有一个这样的表,称为
airports
。
id | code | display_name
----+------+----------------------------
1 | SDF | International
2 | INT | International Airport
3 | TES | Test
4 | APP | Airport Place International
In short, I have a query in a controller method that gets called asynchronously when a user text searches for an airport either by code
or display_name
. 简而言之,我在控制器方法中有一个查询,当用户文本通过
code
或display_name
搜索机场时,该查询将被异步调用。 However, when a user types in an input that matches a code
exactly (airport code is unique), I want that result to appear first, and all airports that also have int
in their display_name
to be displayed afterwards in ascending order. 但是,当用户输入与
code
完全匹配的输入(机场代码是唯一的)时,我希望该结果首先出现,并且在display_name
中也具有int
所有机场随后将以升序显示。 If there is no exact match, it should return any wildcard matches sorted by display_name
ascending. 如果不存在完全匹配项,则应返回按
display_name
升序排序的所有通配符匹配项。 So if a user types in INT
, The row (2, INT, International Airport)
should be returned first followed by the others: 因此,如果用户键入
INT
,则应首先返回行(2, INT, International Airport)
然后再返回其他行:
Results:
1. INT | International Airport
2. APP | Airport Place International
3. SDF | International
Here's the kind of query I was tinkering with that is slightly simplified to make sense outside the context of my application but same concept nonetheless. 这是我正在修改的查询的一种,它在我的应用程序上下文之外进行了稍微简化以使其有意义,但是仍然具有相同的概念。
SELECT * FROM airports
WHERE display_name LIKE 'somesearchtext%'
ORDER BY (CASE WHEN a.code = 'somesearchtext` THEN a.code ELSE a.display_name END)
Right now the results if I type INT
I'm getting 现在,如果我输入
INT
,结果就是
Results:
1. APP | Airport Place International
2. INT | International Airport
3. SDF | International
My ORDER BY
must be incorrect but I can't seem to get it Any help would be greatly appreciated :) 我的
ORDER BY
必须不正确,但我似乎无法得到它。任何帮助将不胜感激:)
If you want an exact match on code
to return first, then I think this does the trick: 如果您希望
code
完全匹配首先返回,那么我认为这可以解决问题:
SELECT a.*
FROM airports a
WHERE a.display_name LIKE 'somesearchtext%'
ORDER BY (CASE WHEN a.code = 'somesearchtext' THEN 1 ELSE 2 END),
a.display_name
You could also write this as: 您也可以这样写:
ORDER BY (a.code = 'somesearchtext') DESC, a.display_name
This isn't standard SQL, but it is quite readable. 这不是标准的SQL,但可读性强。
I think you can achieve your goal by using a UNION. 我认为您可以通过使用UNION来实现您的目标。 First get an exact match and then add that result to rest of the data as you which.
首先获得完全匹配,然后将该结果添加到其余数据中。
eg. 例如。 (you will need to work in this a bit)
(您将需要进行一些工作)
SELECT * FROM airports
WHERE code == 'somesearchtext'
ORDER BY display_name
UNION
SELECT * FROM airports
WHERE code != 'somesearchtext' AND display_name LIKE 'somesearchtext%'
ORDER BY display_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.