Sql - join without duplicate key

I want to join two tables with several hundred columns, for example:

select * from a, b where a.key = b.key

      

The problem is I am getting a table with

Key | Key | Row1 | etc...

      

Without explicitly labeling all columns (" select a.key, row1, ... from a, b where a.key = b.key

"), is there a way to constrain the query so that it only returns one of the keys?

+3


source to share


3 answers


select * from a INNER JOIN b USING (key)

      



The USING statement makes the key appear only once in your result.

+6


source


Perhaps NATURAL JOIN is the solution for you:

SELECT * FROM a NATURAL JOIN b;

      



But if there are more duplicate key names, and you want both of these keys in the results, then natural join is not good for you.

+3


source


If you specifically name your fields instead of *, you won't have duplicate keys and the query will be faster (so I heard).

select key, field1, field2, field3 from a, b where a.key = b.key

      

+1


source







All Articles