[英]SQL SELECT JOIN COLUMN ALIAS
Thanks in advance for your help: 在此先感谢您的帮助:
[People]
table has two columns: [People]
表有两列:
[Marriages]
table has three columns: [Marriages]
表有三列:
I want to SELECT the following columns: 我想选择以下列:
I think I need to use a SELECT
statement with a JOIN
, but I'm not sure how to assign a unique alias to each PersonName
. 我想我需要使用带有
JOIN
的SELECT
语句,但我不确定如何为每个PersonName
分配一个唯一的别名。 This is what I have so far (that doesn't work): 这是我到目前为止(这不起作用):
SELECT
[Marriages].[MarriageID],
[People].[PersonName] AS aName,
[People].[PersonName] AS bName
FROM
[Marriages]
JOIN
[People] ON [Marriages].[PersonIDa] = [People].[PersonID]
JOIN
[People] ON [Marriages].[PersonIDb] = [People].[PersonID]
Thanks again... 再次感谢...
Does this match what you're trying to accomplish? 这符合您要完成的任务吗?
SELECT m.[MarriageID], peopleA.[PersonName] AS aName, peopleB.[PersonName] AS bName
FROM [Marriages] M
JOIN [People] peopleA ON m.[PersonIDa] = peopleA.[PersonID]
JOIN [People] peopleB ON m.[PersonIDb] = peopleB.[PersonID]
I think you're trying to do this... 我想你正试图这样做......
SELECT
m.[MarriageID],
pa.[PersonName] AS aName,
pb.[PersonName] AS bName
FROM [Marriages] m
JOIN [People] pa ON m.[PersonIDa] = pa.[PersonID]
JOIN [People] pb ON m.[PersonIDb] = pb.[PersonID];
You will need something like: 你需要这样的东西:
Select MarriageID, a.PersonName, b.PersonName
From Marriages m
Join People a On a.PersonID = m.Person1ID
Join People b on b.PersonID = m.Person2ID
Don't forget what type of JOIN you need..."INNER JOIN" / "LEFT OUTER JOIN" / "RIGHT OUTER JOIN"/ "CROSS JOIN" 不要忘记你需要什么类型的JOIN ......“INNER JOIN”/“LEFT OUTER JOIN”/“RIGHT OUTER JOIN”/“CROSS JOIN”
SELECT MarriageID, a.PersonName, b.PersonName
FROM Marriages m JOIN People a ON a.PersonID = m.Person1ID
JOIN People b ON b.PersonID = m.Person2ID
Thanks everyone; 感谢大家; your suggestions worked perfectly.
你的建议完美无缺。 I was trying to create an alias for the repeated column in the SELECT statement when I should have been trying to create an alias for the repeated table in the JOIN statement.
当我试图在JOIN语句中为重复表创建别名时,我试图在SELECT语句中为重复列创建别名。
For anyone else who runs into this: 对于遇到此问题的其他人:
SELECT
[TABLE1 NAME].[TABLE1 COLUMN],
(TABLE2 ALIAS1).[TABLE2 COLUMN],
(TABLE2 ALIAS2).[TABLE2 COLUMN],
FROM [TABLE1 NAME]
JOIN [TABLE2 NAME] (TABLE2 ALIAS1) ON [TABLE1 NAME].[TABLE1 MATCHING COLUMN] = (TABLE2 ALIAS1).[TABLE2 MATCHING COLUMN]
JOIN [TABLE2 NAME] (TABLE2 ALIAS2) ON [TABLE1 NAME].[TABLE1 MATCHING COLUMN] = (TABLE2 ALIAS2).[TABLE2 MATCHING COLUMN]
The alias name is created in the JOIN statement and applied to where it is used in the SELECT statement, not the other way around. 别名在JOIN语句中创建,并应用于SELECT语句中的使用位置,而不是相反。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.