The problem is not union , it select a.*, b.* In each of the internal select statements, since a and b have B_id columns, which means that you have two B_id columns as a result.
You can fix this by changing the selection to something like:
select a.*, b.col_1, b.col_2
In general, I would avoid using select table1.* In queries that you use from code (and not just for interactive queries). If someone adds a column to the table, various queries may suddenly stop working.
source share