如何赋予完整匹配相对于部分匹配的权重 (PostgreSQL)
我有一个查询,它需要输入搜索包含数万个条目(应该几乎是美国的每个城市)的位置表中邮政编码/地区/城市/地铁代码中最匹配的匹配项。我使用的查询是:
select
metrocode,
region,
postalcode,
region_full,
city
from
dv_location
where
(
region ilike '%Chicago%'
or
postalcode ilike '%Chicago%'
or
city ilike '%Chicago%'
or
region_full ilike'%Chicago%'
)
and metrocode is not null
奇怪的是,我得到的结果集看起来像这样:
metrocode;region;postalcode;region_full;city
862;CA;95712;California;Chicago Park
862;CA;95712;California;Chicago Park
602;IL;60611;Illinois;Chicago
602;IL;60610;Illinois;Chicago
我做错了什么?我的想法是,芝加哥比芝加哥公园具有更大的权重,因为芝加哥与该术语完全匹配(尽管我要求对该术语进行通配符匹配)。
I've got a query that takes an input searches for the closet match in zipcode/region/city/metrocode in a location table containing a few tens of thousands of entries (should be nearly every city in the US). The query I'm using is:
select
metrocode,
region,
postalcode,
region_full,
city
from
dv_location
where
(
region ilike '%Chicago%'
or
postalcode ilike '%Chicago%'
or
city ilike '%Chicago%'
or
region_full ilike'%Chicago%'
)
and metrocode is not null
Odd thing is, the results set I'm getting back looks like this:
metrocode;region;postalcode;region_full;city
862;CA;95712;California;Chicago Park
862;CA;95712;California;Chicago Park
602;IL;60611;Illinois;Chicago
602;IL;60610;Illinois;Chicago
What am I doing wrong? My thinking is that Chicago would have greater weight than Chicago Park since Chicago is an exact match to the term (even though I'm asking for a wildcard match on the term).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 order by 子句中显式设置顺序。
或者您希望对结果进行排序...
You can set the order explicitly in the order by clause.
or however you would like your results to be ordered ...
试试这个:
Try this: