Skip to content
Advertisement

postgresql how to choose specific column from exact same columns?

I have table A and B. If I inner join them like

SELECT * FROM A INNER JOIN B on A.a = B.a 

The new table has two exactly the same columns “a”. How do I choose the first column of “a”? Or how do I avoid generate two same columns after inner join?

Advertisement

Answer

You can use an alias for each column such as:

select
  a.id,
  a.firstname as a_firstname,
  b.firstname as b_firstname
from a inner join b on a.id = b.id

That way, for matching ID=1, if firstname is ‘John’ in table a but ‘Jon’ in table b, you can print them appropriately.

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