如果有可能有人用这个例子告诉我吗?
SELECT DISTINCT
S.SheetID,
IF.MOTHER,
IF.FATHER,
IF.SON,
IF.DAUGHTER,
FM.GRANDFATHER,
FM.GRANDMOTHER,
FM.UNCLE,
FM.AUNT
FROM
Family.dbo.family IF
LEFT JOIN
Family.dbo.fatherandmothersiderelatives FM ON S.SheetID = FM.FamilyID
LEFT JOIN
FammilyFiles.dbo.[Sheet] S ON S.SheetID = IF.FamilyID
答案 0 :(得分:1)
是的,你可以,但你需要按照他们引用的顺序连接表。在您的示例中,Family.dbo.fatherandmothersiderelatives FM ON S.SheetID = FM.FamilyID
在加入和别名之前引用Sheet s
(并且您应该收到错误,例如多部分标识符" S.SheetID&#34 ;无法绑定。)。您需要更改顺序:
SELECT DISTINCT
S.SheetID,
[IF].MOTHER,
[IF].FATHER,
[IF].SON,
[IF].DAUGHTER,
FM.GRANDFATHER,
FM.GRANDMOTHER,
FM.UNCLE,
FM.AUNT
FROM
Family.dbo.family [IF]
LEFT JOIN
Family.dbo.[Sheet] S ON S.SheetID = [IF].FamilyID
LEFT JOIN
FamilyFiles.dbo.fatherandmothersiderelatives FM ON S.SheetID = FM.FamilyID
同样IF
是一个保留关键字,因此对于表别名来说是一个非常糟糕的选择 - 如果你真的想要使用它,你可以将它括在上面的括号中。