2

我正在编写一个搜索客户端,以查询 postgis db。

搜索查询将是,

    SELECT tags->'name' AS name,ST_Transform(way,4326) AS pt_lonlattext 
FROM planet_osm_point WHERE lower(tags->'name') LIKE '%school%'

现在,我需要这样的结果,

完全匹配的结果应优先于部分匹配的结果。例如,名称中只有“学校”的结果应该在返回列表中的第一位,然后是“小学”、“高中”等的结果。

有没有办法重新构造上述查询以获得所需的结果?

4

1 回答 1

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
于 2013-09-02T14:43:37.190 回答