Skip to content
Advertisement

Doing a union on two tables, but renaming columns that may have the same name in postgres and dropping others

I am doing a union on two tables that have some of the same column names. So, say they look something like this:

Here is what I am currently doing:

Currently I obviously get repeated columns. So here is what I want to do:

1- The table2’s columns name and details, I would like to rename to t1_name and t2_details respectively

2- I would like to drop table2’s _id, fk_id, createdAt and updatedAt properties. How do I do that?

Advertisement

Answer

Just enumerate the columns that you want to retain, and alias them as needed:

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