我有这段代码:
SELECT A.UNITCODE, B.FORMATIONCODE, C.UPPERFORMATIONCODE, D.UPPERFORMATIONCODE
FROM UNIT AS A.UNITCODE
INNER JOIN FORMATION AS B.FORMATIONCODE
INNER JOIN UPPERFORMATION_UNIT AS C.UPPERFORMATION
INNER JOIN UPPERFORMATION AS D.UPPERFORMATIONCODE
WHERE UNITCODE='7000007'
你们能帮助我如何用指定的列加入4个表吗?
答案 0 :(得分:1)
确定所有3个相关表具有相同的UNIT_ID以便与表UNIT
连接 SELECT
A.UNITCODE
, B.FORMATIONCODE
, C.UPPERFORMATIONCODE
, D.UPPERFORMATIONCODE
FROM UNIT AS A
INNER JOIN FORMATION AS B ON B.FORMATIONCODE = A.UNIT_ID
INNER JOIN UPPERFORMATION_UNIT AS C. C.UPPERFORMATION = A.UNIT_ID
INNER JOIN UPPERFORMATION AS D D.UPPERFORMATIONCODE = A.UNIT_ID
WHERE UNITCODE='7000007'
答案 1 :(得分:0)
您似乎在混淆表别名和链接列。
这是如何在查询中为表提供别名以增强可读性:
INNER JOIN formation AS f
AS
是可选的。大多数情况下它都被忽略了。
这是如何加入:
FROM unit AS u
INNER JOIN upperformation_unit AS ufu ON ufu.unitcode = u.unitcode
好吧,我当然不知道连接表格的列,更不用说他们的名字了。但我认为查询必须或多或少看起来像这样:
SELECT
u.unitcode,
f.formationcode,
uf.upperformationcode,
ufu.upperformationcode
FROM unit u
JOIN upperformation_unit ufu ON ufu.unitcode = u.unitcode
JOIN upperformation uf ON uf.upperformationcode = ufu.upperformationcode
JOIN formation f ON f.formationcode uf.formationcode
WHERE u.unitcode = 7000007;