问题
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.
回答1:
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
回答2:
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
回答3:
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 *.
来源:https://stackoverflow.com/questions/8244750/sql-select-column-when-theres-more-than-one-of-the-same-name