Rrobinvip
Rrobinvip

Reputation: 119

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?

Upvotes: 0

Views: 41

Answers (2)

zedfoxus
zedfoxus

Reputation: 37039

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.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269563

It doesn't matter, they are the same.

But if you don't want duplicates, then using does that for you:

SELECT *
FROM A INNER JOIN
     B
     USING (a)

Upvotes: 1

Related Questions