suppose I have two tables A
and B
, both with only 1 column name
. Some rows appear in both tables while some are exclusive to one table. I want to union these two tables together into a table C
that has 3 columns name
, is_in_A
and is_in_B
. For a row that appeared in both A
and B
, the last 2 fields would both be 'true'. For a row that appeared in only A
, only is_in_A
would be 'true' and is_in_B
would be false.
My current solution works like this:
Create three temporary tables that all have the columns name
, is_in_A
and is_in_B
:
A_join_B
that has all rows that are inA
andB
.is_in_A
andis_in_B
are both true for every row in this tableA_minus_B
that has all rows that are exclusive toA
.is_in_A
is true andis_in_B
is false for every row in that table.B_minus_A
respectively.
Now I union all 3 tables together to get my result.
Is there a better/easier way to do this (ideally as a single union without any temporary tables)?