简体   繁体   English

多个相同名称的SQL选择列

[英]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. 我想是这样,因为有1个以上的同名列。

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. 普通联接将不起作用,因为它会删除其他表中没有属性的某些字段。

You can use the 'AS' keyword to name a column. 您可以使用“ AS”关键字来命名列。 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
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

Not sure if it's Oracle only, but USING can do this for you for ad-hoc queries: 不确定是否仅是Oracle,但是USING可以为您执行即席查询:

SELECT *
FROM TABLEA
JOIN TABLEB USING (NAME)

This will only return one NAME column from the SELECT *. 这只会从SELECT *返回一个NAME列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM