提前感谢您的帮助:
[People]
表有两列:
[Marriages]
表有三列:
我想选择以下列:
我想我需要使用带有SELECT
的{{1}}语句,但我不确定如何为每个JOIN
分配唯一的别名。这是我到目前为止(这不起作用):
PersonName
再次感谢...
答案 0 :(得分:8)
这是否符合您要完成的任务?
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]
答案 1 :(得分:2)
我想你正试图这样做......
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];
答案 2 :(得分:1)
您将需要以下内容:
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
答案 3 :(得分:1)
不要忘记你需要什么类型的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
答案 4 :(得分:-1)
谢谢大家;你的建议完美无缺。当我试图在JOIN语句中为重复表创建别名时,我试图在SELECT语句中为重复列创建别名。
对于遇到此问题的其他人:
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]
别名在JOIN语句中创建,并应用于SELECT语句中的使用位置,而不是相反。