我很难两次加入相同的表格,因此返回的结果完全不正确。
下面的查询工作得很好。但是,我想更改它,以便我可以使用Requirement Type
列中返回的值返回Requirement Traced To
的额外列。
SELECT R.RQ_REQ_ID as "Requirement Traced From",
R.RQ_REQ_NAME as "Requirement Name",
RTY.TPR_NAME as "Requirement Type",
RTR.RT_TO_REQ_ID as "Requirement Traced To"
FROM REQ R
LEFT JOIN REQ_TRACE RTR
ON R.RQ_REQ_ID = RTR.RT_FROM_REQ_ID, Req_Type RTY
WHERE R.RQ_TYPE_ID = RTY.TPR_TYPE_ID
AND RTY.TPR_NAME in ('TOM', 'Business Process Map', 'Work Instruction', 'Functional', 'Customer Journey', 'Business')
ORDER BY 1
当我第二次使用不同的别名添加REQ
和REQ_TYPE
表时,我会返回数百行,而不是我期待的28行。
任何帮助将不胜感激。
答案 0 :(得分:1)
从不在FROM
子句中使用逗号。 始终使用明确的JOIN
语法。
您需要添加以下附加联接:
SELECT R.RQ_REQ_ID as "Requirement Traced From",
R.RQ_REQ_NAME as "Requirement Name",
RTY.TPR_NAME as "Requirement Type",
RTR.RT_TO_REQ_ID as "Requirement Traced To",
RTY2.TPR_NAME as "Requirement Type To",
FROM REQ R LEFT JOIN
REQ_TRACE RTR
ON R.RQ_REQ_ID = RTR.RT_FROM_REQ_ID LEFT JOIN
Req_Type RTY
ON R.RQ_TYPE_ID = RTY.TPR_TYPE_ID LEFT JOIN
REQ R R2
ON R2.RQ_REQ_ID = RTR.RT_TO_REQ_ID LEFT JOIN
Req_Type RTY2
ON RTY2.TPR_TYPE_ID = R2.RQ_TYPE_ID
WHERE RTY.TPR_NAME in ('TOM', 'Business Process Map', 'Work Instruction', 'Functional', 'Customer Journey', 'Business')
ORDER BY 1;
答案 1 :(得分:0)
使用逗号后,您需要使用两个join
而不是在连接条件后写表名:
SELECT R.RQ_REQ_ID as "Requirement Traced From",
R.RQ_REQ_NAME as "Requirement Name",
RTY.TPR_NAME as "Requirement Type",
RTR.RT_TO_REQ_ID as "Requirement Traced To"
FROM REQ R
LEFT JOIN REQ_TRACE RTR
ON R.RQ_REQ_ID = RTR.RT_FROM_REQ_ID
LEFT JOIN Req_Type RTY
ON R.RQ_TYPE_ID = RTY.TPR_TYPE_ID
and RTY.TPR_NAME in ('TOM', 'Business Process Map', 'Work Instruction', 'Functional', 'Customer Journey', 'Business')
ORDER BY 1