Skip to content
Advertisement

Select from cross tables in postgres problem

Lets imagine i have a table A that contains rows X,Y and Z, and i have another table that relate elements from A to another table B.

A_B : element_A, element_B, and i want a query that for element b in B returns for all elements a in A return True if {a, b} exist in the table A_B and False if not

and that’s what i tried so far.

Advertisement

Answer

You can cross join tables a and b to generate all possible combinations, then bring bridge table a_b with a left join:

You could also use exists and a correlated subquery:

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