SQL - Table 左连接中的别名
SQL - Table alias in Left Join
我正在尝试使用 Left Join 编写一个简单的 SQL 查询。
这是查询:
SELECT *
FROM (
SELECT *
FROM TRN_IN.COIT AS TRANSMISSIONS
LEFT JOIN (
SELECT TRNNumber ,COUNT(ID)
FROM TRN_IN.COIT AS TOTAL_LINES
WHERE DataPiece02 = 'TO'
GROUP BY TRNNumber
) ON TRANSMISSIONS.TRNNumber = TOTAL_LINES.TRNNumber
)
第二个 table 别名是 TOTAL_LINES。
当 运行 连接它时,我收到一条错误消息,指出 TOTAL_LINES 未列出。
奇怪的是,当尝试 运行 第二个查询本身时,没有错误:
(SELECT TRNNumber ,COUNT(ID)
FROM TRN_IN.COIT AS TOTAL_LINES
WHERE DataPiece02 = 'TO'
GROUP BY TRNNumber )
有人知道吗?
希望您在错误的地方分配了 AS TOTAL_LINES
table 别名。
在第二个子查询后放置 table 别名将解决您的问题。
你能试试下面的查询吗:
SELECT *
FROM (
SELECT *
FROM TRN_IN.COIT AS TRANSMISSIONS
LEFT JOIN (
SELECT TRNNumber
,COUNT(ID) AS CountDetails
FROM TRN_IN.COIT
WHERE DataPiece02 = 'TO'
GROUP BY TRNNumber
) AS TOTAL_LINES ON TRANSMISSIONS.TRNNumber = TOTAL_LINES.TRNNumber
) AS Result
TOTAL_LINES 必须是整个 select 的别名,而不是在 from 子句中的 table 上。更改为:
SELECT * FROM ( SELECT * FROM TRN_IN.COIT AS TRANSMISSIONS
LEFT JOIN ( SELECT TRNNumber ,COUNT(ID) FROM TRN_IN.COIT WHERE DataPiece02 = 'TO' GROUP BY TRNNumber ) AS TOTAL_LINES
ON TRANSMISSIONS.TRNNumber = TOTAL_LINES .TRNNumber)
试试这个..
SELECT TOTAL_LINES.TRNNumber,COUNT(TOTAL_LINES.ID) FROM TRN_IN.COIT TRANSMISSIONS LEFT JOIN TRN_IN.COIT TOTAL_LINES
ON TRANSMISSIONS.TRNNumber = TOTAL_LINES.TRNNumber
WHERE TOTAL_LINES.DataPiece02 = 'TO' GROUP BY TOTAL_LINES.TRNNumber;
我正在尝试使用 Left Join 编写一个简单的 SQL 查询。
这是查询:
SELECT *
FROM (
SELECT *
FROM TRN_IN.COIT AS TRANSMISSIONS
LEFT JOIN (
SELECT TRNNumber ,COUNT(ID)
FROM TRN_IN.COIT AS TOTAL_LINES
WHERE DataPiece02 = 'TO'
GROUP BY TRNNumber
) ON TRANSMISSIONS.TRNNumber = TOTAL_LINES.TRNNumber
)
第二个 table 别名是 TOTAL_LINES。
当 运行 连接它时,我收到一条错误消息,指出 TOTAL_LINES 未列出。
奇怪的是,当尝试 运行 第二个查询本身时,没有错误:
(SELECT TRNNumber ,COUNT(ID)
FROM TRN_IN.COIT AS TOTAL_LINES
WHERE DataPiece02 = 'TO'
GROUP BY TRNNumber )
有人知道吗?
希望您在错误的地方分配了 AS TOTAL_LINES
table 别名。
在第二个子查询后放置 table 别名将解决您的问题。
你能试试下面的查询吗:
SELECT *
FROM (
SELECT *
FROM TRN_IN.COIT AS TRANSMISSIONS
LEFT JOIN (
SELECT TRNNumber
,COUNT(ID) AS CountDetails
FROM TRN_IN.COIT
WHERE DataPiece02 = 'TO'
GROUP BY TRNNumber
) AS TOTAL_LINES ON TRANSMISSIONS.TRNNumber = TOTAL_LINES.TRNNumber
) AS Result
TOTAL_LINES 必须是整个 select 的别名,而不是在 from 子句中的 table 上。更改为:
SELECT * FROM ( SELECT * FROM TRN_IN.COIT AS TRANSMISSIONS
LEFT JOIN ( SELECT TRNNumber ,COUNT(ID) FROM TRN_IN.COIT WHERE DataPiece02 = 'TO' GROUP BY TRNNumber ) AS TOTAL_LINES
ON TRANSMISSIONS.TRNNumber = TOTAL_LINES .TRNNumber)
试试这个..
SELECT TOTAL_LINES.TRNNumber,COUNT(TOTAL_LINES.ID) FROM TRN_IN.COIT TRANSMISSIONS LEFT JOIN TRN_IN.COIT TOTAL_LINES
ON TRANSMISSIONS.TRNNumber = TOTAL_LINES.TRNNumber
WHERE TOTAL_LINES.DataPiece02 = 'TO' GROUP BY TOTAL_LINES.TRNNumber;