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

百般思念 提交于 2019-12-13 03:39:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!