gelatine1
gelatine1

Reputation: 199

inner join creating a duplicate column

I have two tables both containing the column artistid. When I use inner join to combine the two tables I get a resulting table that contains the column artistid twice making me unable to retrieve the artistid (as it complains about being ambiguous). How do I make sure the same column doesn't reappear after combining the tables?
This is the query I used:

SELECT * FROM artist a INNER JOIN track b ON a.artistid = b.artistid

Upvotes: 2

Views: 2330

Answers (2)

please do not use inner join. I had the similar problem creating report. Use instead. SELECT track from atrist where a.artistid = b.artistid

Upvotes: -1

Lukasz Szozda
Lukasz Szozda

Reputation: 175566

If the only duplicate column is artistid (column that you use for joining) you can use:

SELECT * 
FROM artist  
JOIN track USING(artistid);

SqlFiddleDemo

Otherwise you need to specify all column and add aliases if needed:

SELECT a.col1 AS alias, a.col2, ..., b.col1 AS alias2, b.col2, ... 
FROM artist a 
INNER JOIN track b ON a.artistid = b.artistid

Upvotes: 5

Related Questions