Home > Mobile >  How to get rows fitting list of parameters in SQL?
How to get rows fitting list of parameters in SQL?

Time:12-31

I have DB with some columns and I have N parameters. I need to get rows where columns have all N parameters in any column.

Example:

id  title           place     author
1  'Book Example1' 'shelves' 'John Johnson'
2  'Book Example2' 'library' 'John Johnson'

And parameters like ['shelves', 'John Johnson'] so output will be row with 1 id.

Can anyone help me with this? Have no idea how to do it by SQL

CodePudding user response:

A simple alternative of full text search. Shape each row into a single text string and match it against the array of parameters ('{shelves,John Johnson}'::text[] in the illustration below) as regular expressions.

select * from the_table t 
 where t::text ~* all('{shelves,John Johnson}'::text[]);
  • Related