Postgres how can I get column inside a quote

70 Views Asked by At

First and foremost I am using postgres version 9.4 . I am trying to create a partial index on this query

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

My issue is that I do not know how to put columns inside the quotes '%' so that the partial index looks like this

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

What is the correct method to put columns inside a single quote mark ? I have been looking all over the place. Right now my partial index looks like this

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

Which is obviously not helping because postgres is treating the 'city%' and state ilike 'state%' as variables and not columns. I have a search-box where users type in the city and state fields so I want to optimize. Any help would be appreciated ...

1

There are 1 best solutions below

0
On

try this

 WHERE column_name LIKE column_name||'%'

see this Link