canisrufus
canisrufus

Reputation: 693

sql - join without duplicating the key

I want to join two tables with several hundred columns, like this:

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

The problem is that I get a table that has

Key | Key | Row1 | etc...

Without naming all of the columns explicitly ("select a.key, row1, ... from a, b where a.key = b.key"), is there a way I can limit the query so that it only returns one of the keys?

Upvotes: 13

Views: 12695

Answers (3)

Jarosław Gomułka
Jarosław Gomułka

Reputation: 4995

Maybe NATURAL JOIN is solution for you:

SELECT * FROM a NATURAL JOIN b;

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

Upvotes: 4

Hiro2k
Hiro2k

Reputation: 5587

select * from a INNER JOIN b USING (key)

The USING statement causes the key to only show up once in your result.

Upvotes: 23

Kristian
Kristian

Reputation: 21830

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

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

Upvotes: 0

Related Questions