mrbunyrabit
mrbunyrabit

Reputation: 509

SQL select column when there's more than one of the same name

I have this query

select *
from alldistros t1
LEFT join origin t2 on t1.name=t2.name
LEFT join desktop t3 on t2.name=t3.name
LEFT join beginnerdistributions t4 on t3.name=t4.name

it add on all my tables. But now when I want to select the name field (which is in all of them) I can't show it. It's just blank when I call it. And I would think so since there's more than 1 columns of the same name.

What can I do to fix this?

Just a plain join won't work, since it removes some of the fields that does not have the properties in the other tables.

Upvotes: 0

Views: 10726

Answers (3)

orbfish
orbfish

Reputation: 7731

Not sure if it's Oracle only, but USING can do this for you for ad-hoc queries:

SELECT *
FROM TABLEA
JOIN TABLEB USING (NAME)

This will only return one NAME column from the SELECT *.

Upvotes: 0

ajh4
ajh4

Reputation: 81

select
  t1.name as t1_name,
  t2.name as t2_name,
  t3.name as t3_name
from alldistros t1
LEFT join origin t2 on t1.name=t2.name 
LEFT join desktop t3 on t2.name=t3.name 
LEFT join beginnerdistributions t4 on t3.name=t4.name

Upvotes: 1

Kevin Gosse
Kevin Gosse

Reputation: 39007

You can use the 'AS' keyword to name a column. For instance:

select t1.name AS DistroName, t2.name AS OriginName, t3.name AS DesktopName
from alldistros t1
LEFT join origin t2 on t1.name=t2.name
LEFT join desktop t3 on t2.name=t3.name
LEFT join beginnerdistributions t4 on t3.name=t4.name

Upvotes: 3

Related Questions