I have columns title, description, keywords and I want to search a text in them.
I’m using PostgreSQL.
SELECT title FROM products
WHERE
ANY(ARRAY(title, description, keywords) like '%test%')
I’m getting error syntax error at or near "ANY".
I know I can do it with OR but I think that would not be the best way to do it.
I think I’m having errors at converting columns to array. Can someone give me an idea?
Thanks!
Advertisement
Answer
Presumably, your goal is to only have the comparison '%test%' appear once. Here is one method:
WHERE EXISTS (SELECT 1
FROM UNNEST(ARRAY[p.title, p.description, p.keywords]) x
WHERE x LIKE '%test%'
)
Note that if you using equality, then IN suffices:
WHERE test IN (p.title, p.description, p.keywords)