如何正确表达内部连接
How to correctly word an inner join
我正在尝试学习联接并已在此处 (SOFlow) 和其他几个地方进行了大量阅读,当然还复制了代码并进行了尝试。
所以我编写了适合我的代码 table:
SELECT
a.FIRSTNAME, a.LASTNAME,
b.[LINE1], b.[LINE2], b.[LINE3], b.[SUBURB],
b.[STATE], b.[POSTCODE],
p.[PRIVATE], p.[BUSINESS], p.[MOBILE]
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]) a
INNER JOIN
(SELECT
[LINE1], [LINE2], [LINE3], [SUBURB], [STATE], [POSTCODE], PARTYID
FROM
[PTAddresses]
WHERE
ADDRESS_TYPE = 'Mailing') b ON a.Uniqid = b.Partyid
INNER JOIN
(SELECT
[Uniqid], [PRIVATE], [BUSINESS], [MOBILE]
FROM [PTPhone]) P ON a.Uniqid = P.Uniqid
WHERE
a.UNIQID = 4
但我得到:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.UNIQID" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.FIRSTNAME" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.LASTNAME" could not be bound.
这段代码执行完美的地方
SELECT
a.FIRSTNAME, a.LASTNAME,
b.[LINE1], b.[LINE2], b.[LINE3], b.[SUBURB], b.[STATE], b.[POSTCODE],
p.[PRIVATE], p.[BUSINESS], p.[MOBILE]
FROM
(SELECT
PTPARTY.UNIQID, PTPARTY.FIRSTNAME, PTPARTY.LASTNAME
FROM [PTPARTY]) a
INNER JOIN
(SELECT
[LINE1], [LINE2], [LINE3], [SUBURB], [STATE], [POSTCODE], PARTYID
FROM
[PTAddresses]
WHERE
ADDRESS_TYPE = 'Mailing') b ON a.Uniqid = b.Partyid
INNER JOIN
(SELECT
[Uniqid], [PRIVATE], [BUSINESS], [MOBILE]
FROM [PTPhone]) P ON a.Uniqid = P.Uniqid
WHERE
a.UNIQID = 4
我 99% 确定无效的代码是从这里复制的,并进行了更改以适合我的 table 当然。
只是想知道我是否做错了什么。
嗯,我确定我做错了什么,但想知道是什么。
向大家致以诚挚的问候,继续努力
您正在限定子查询中的列,但没有相应的 table 别名:
Select p.FIRSTNAME, p.LASTNAME, a.[LINE1],
a.[LINE2], a.[LINE3], a.[SUBURB], a.[STATE], a.[POSTCODE],
ph.[PRIVATE], ph.[BUSINESS], ph.[MOBILE]
from PTPARTY p inner join
PTAddresses a
on p.Uniqid = a.Partyid inner join
PTPhone ph
on p.Uniqid = Ph.Uniqid
where a.ADDRESS_TYPE = 'Mailing' and p.UNIQID = 4;
这个真的简单多了。
请注意,我将 table 别名替换为含义别名。但是,您不需要子查询:
检查此代码。这会起作用。该错误是由于第一个子查询引起的,因为在第一个子查询中您没有为 table 添加别名,而是使用了 alias.column 名称。所以将其更改为以下内容:
Select a.FIRSTNAME,a.LASTNAME, b.[LINE1],b.[LINE2],b.[LINE3],b.[SUBURB],b.[STATE],b.[POSTCODE],p.[PRIVATE],p.[BUSINESS],p.[MOBILE]
from
(SELECT UNIQID,FIRSTNAME,LASTNAME FROM [PTPARTY]) a
inner join
(SELECT [LINE1],[LINE2],[LINE3],[SUBURB],[STATE],[POSTCODE],PARTYID FROM [PTAddresses]
where ADDRESS_TYPE = 'Mailing') b on a.Uniqid = b.Partyid
inner join
(SELECT [Uniqid],[PRIVATE],[BUSINESS],[MOBILE]FROM [PTPhone]) P on a.Uniqid = P.Uniqid
where a.UNIQID = 4
要帮助非常具体:
考虑这部分查询:
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]) a
当您从子查询中删除括号时,您会得到:
SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]
项目a.UNIQUID
的格式为table别名。列名。但是,括号内,您没有为 [PTPARTY] 指定 table 别名。因此,a.UNIQUID
无效。
因为只有 1 个 table,别名或 table 引用是不必要的。为了清楚起见,我将从这里开始添加 optional "AS"。只有 1 table,您可以仅使用列名来执行此操作:
FROM
(SELECT
UNIQID, FIRSTNAME, LASTNAME
FROM [PTPARTY]) AS a
当然,您经常加入 tables 并希望使用别名,因此您可以在括号内这样做:
FROM
(SELECT
z.UNIQID, z.FIRSTNAME, z.LASTNAME
FROM [PTPARTY] AS z) AS a
在这种情况下,table 别名 "z" 不在括号外 available/valid。出于这个原因,即使这可能不是最佳实践,您甚至可以重复使用相同的别名。不过,这可能会变得令人困惑。
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY] AS a) AS a
我正在尝试学习联接并已在此处 (SOFlow) 和其他几个地方进行了大量阅读,当然还复制了代码并进行了尝试。
所以我编写了适合我的代码 table:
SELECT
a.FIRSTNAME, a.LASTNAME,
b.[LINE1], b.[LINE2], b.[LINE3], b.[SUBURB],
b.[STATE], b.[POSTCODE],
p.[PRIVATE], p.[BUSINESS], p.[MOBILE]
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]) a
INNER JOIN
(SELECT
[LINE1], [LINE2], [LINE3], [SUBURB], [STATE], [POSTCODE], PARTYID
FROM
[PTAddresses]
WHERE
ADDRESS_TYPE = 'Mailing') b ON a.Uniqid = b.Partyid
INNER JOIN
(SELECT
[Uniqid], [PRIVATE], [BUSINESS], [MOBILE]
FROM [PTPhone]) P ON a.Uniqid = P.Uniqid
WHERE
a.UNIQID = 4
但我得到:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.UNIQID" could not be bound.Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.FIRSTNAME" could not be bound.Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.LASTNAME" could not be bound.
这段代码执行完美的地方
SELECT
a.FIRSTNAME, a.LASTNAME,
b.[LINE1], b.[LINE2], b.[LINE3], b.[SUBURB], b.[STATE], b.[POSTCODE],
p.[PRIVATE], p.[BUSINESS], p.[MOBILE]
FROM
(SELECT
PTPARTY.UNIQID, PTPARTY.FIRSTNAME, PTPARTY.LASTNAME
FROM [PTPARTY]) a
INNER JOIN
(SELECT
[LINE1], [LINE2], [LINE3], [SUBURB], [STATE], [POSTCODE], PARTYID
FROM
[PTAddresses]
WHERE
ADDRESS_TYPE = 'Mailing') b ON a.Uniqid = b.Partyid
INNER JOIN
(SELECT
[Uniqid], [PRIVATE], [BUSINESS], [MOBILE]
FROM [PTPhone]) P ON a.Uniqid = P.Uniqid
WHERE
a.UNIQID = 4
我 99% 确定无效的代码是从这里复制的,并进行了更改以适合我的 table 当然。
只是想知道我是否做错了什么。
嗯,我确定我做错了什么,但想知道是什么。
向大家致以诚挚的问候,继续努力
您正在限定子查询中的列,但没有相应的 table 别名:
Select p.FIRSTNAME, p.LASTNAME, a.[LINE1],
a.[LINE2], a.[LINE3], a.[SUBURB], a.[STATE], a.[POSTCODE],
ph.[PRIVATE], ph.[BUSINESS], ph.[MOBILE]
from PTPARTY p inner join
PTAddresses a
on p.Uniqid = a.Partyid inner join
PTPhone ph
on p.Uniqid = Ph.Uniqid
where a.ADDRESS_TYPE = 'Mailing' and p.UNIQID = 4;
这个真的简单多了。 请注意,我将 table 别名替换为含义别名。但是,您不需要子查询:
检查此代码。这会起作用。该错误是由于第一个子查询引起的,因为在第一个子查询中您没有为 table 添加别名,而是使用了 alias.column 名称。所以将其更改为以下内容:
Select a.FIRSTNAME,a.LASTNAME, b.[LINE1],b.[LINE2],b.[LINE3],b.[SUBURB],b.[STATE],b.[POSTCODE],p.[PRIVATE],p.[BUSINESS],p.[MOBILE]
from
(SELECT UNIQID,FIRSTNAME,LASTNAME FROM [PTPARTY]) a
inner join
(SELECT [LINE1],[LINE2],[LINE3],[SUBURB],[STATE],[POSTCODE],PARTYID FROM [PTAddresses]
where ADDRESS_TYPE = 'Mailing') b on a.Uniqid = b.Partyid
inner join
(SELECT [Uniqid],[PRIVATE],[BUSINESS],[MOBILE]FROM [PTPhone]) P on a.Uniqid = P.Uniqid
where a.UNIQID = 4
要帮助非常具体:
考虑这部分查询:
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]) a
当您从子查询中删除括号时,您会得到:
SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY]
项目a.UNIQUID
的格式为table别名。列名。但是,括号内,您没有为 [PTPARTY] 指定 table 别名。因此,a.UNIQUID
无效。
因为只有 1 个 table,别名或 table 引用是不必要的。为了清楚起见,我将从这里开始添加 optional "AS"。只有 1 table,您可以仅使用列名来执行此操作:
FROM
(SELECT
UNIQID, FIRSTNAME, LASTNAME
FROM [PTPARTY]) AS a
当然,您经常加入 tables 并希望使用别名,因此您可以在括号内这样做:
FROM
(SELECT
z.UNIQID, z.FIRSTNAME, z.LASTNAME
FROM [PTPARTY] AS z) AS a
在这种情况下,table 别名 "z" 不在括号外 available/valid。出于这个原因,即使这可能不是最佳实践,您甚至可以重复使用相同的别名。不过,这可能会变得令人困惑。
FROM
(SELECT
a.UNIQID, a.FIRSTNAME, a.LASTNAME
FROM [PTPARTY] AS a) AS a