Skip to content
Advertisement

query for many to many record matching

I have table tag_store like below

enter image description here

I want to filter the ids which has all tag provided in a like

My desired output is it should have output onlye id 1 as it has all three tg_value and tg_id matched..

Please help, what should I change, or is there any better alternate

Thanks

Advertisement

Answer

I would aggregate the values into a JSON array and use the @> operator to filter those that have all of them:

Online example

You can also do that directly in a HAVING clause if you want


Note that this will return IDs that have additional tags apart from those in the comparison array.

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