Search code examples
sqlcall

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.


Solution

  • 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