Skip to content
Advertisement

How to determine if lists have at least one same element

Product table contains tags columns:

tag columns contain tags separated by semicolon like

How to find all rows where tag1 and tag2 columns contain at least one same tag ?

For example row

should be in result since both columns contain AC

Row

Should not be in result since all tags are different.

Using PostgreSQL 13.2

Advertisement

Answer

You can use the && operator to check if there are common elements in the columns. To do this, you must first convert the string to an array using the string_to_array

Demo in DBfiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement