0

首先,我使用的是 postgres 版本 9.4 。我正在尝试为此查询创建部分索引

select DISTINCT ON(city,state)city,state,zip from zips where city ilike 
'%' and state ilike '%' limit 10

我的问题是我不知道如何将列放在引号 '%'中,以便部分索引看起来像这样

 select DISTINCT ON(city,state)city,state,zip from zips where city ilike 
    '{city}%' and state ilike '{state}%' limit 10

将列放在单引号内的正确方法是什么?我一直在四处寻找。现在我的部分索引看起来像这样

CREATE INDEX "Location_Search"
   ON zips (city ASC NULLS LAST, state ASC NULLS LAST)
   where city ilike 'city%' and state ilike 'state%';

这显然没有帮助,因为 postgres 将'city%' 和状态 ilike 'state%'视为变量而不是列。我有一个搜索框,用户在其中输入城市和州字段,所以我想优化。任何帮助,将不胜感激 ...

4

1 回答 1

0

尝试这个

 WHERE column_name LIKE column_name||'%'

看到这个链接

于 2016-02-06T09:28:15.243 回答