我正在编写一个搜索客户端,以查询postgis db。
搜索查询将是,
SELECT tags->'name' AS name,ST_Transform(way,4326) AS pt_lonlattext
FROM planet_osm_point WHERE lower(tags->'name') LIKE '%school%'
现在,我需要的结果是,
完全匹配的结果应优先于部分匹配的结果。 例如名单中只有“学校”的结果应该在返回的列表中首先显示,然后是“小学”,“高中”等的结果。
有没有办法重新构建上述查询以获得所需的结果?
答案 0 :(得分:2)
这样的事情?
select
tags->'name' as name,
ST_Transform(way,4326) as pt_lonlattext
from planet_osm_point
where lower(tags->'name') LIKE '%school%'
order by case when lower(tags->'name') = 'school' then 0 else 1 end
或更多DRY版本:
with cte as (
select
tags->'name' as name,
ST_Transform(way,4326) as pt_lonlattext
from planet_osm_point
)
select name, pt_lonlattext
from cte
where lower(name) like '%school%'
order by case when lower(name) = 'school' then 0 else 1 end