我有一个像下面这样的查询工作正常。
SELECT
"A".id AS A_id,
"B".id AS B_id,
"C".id AS C_id,
"D".id AS D_id,
"E".id AS E_id,
"F".id AS F_id,
"G".id AS G_id,
"H".id AS H_id,
"I".id AS I_id
FROM "A"
INNER JOIN "B" ON "A".id = "B".A_id
INNER JOIN "C" ON "B".C_id = "C".id
INNER JOIN "D" ON "C".D_id = "D".id
INNER JOIN "E" ON "D".id = "E".D_id
INNER JOIN "F" ON "E".F_id = "F".id
INNER JOIN "G" ON "F".G_id = "G".id
INNER JOIN "H" ON "G".id = "H".G_id
INNER JOIN "I" ON "H".I_id = "I".id;
现在我想添加特殊条件。表" B"与另一个表连接 而且我必须添加特殊连接(完全连接)但是在所有连接之后。像下面的东西 (它当然不起作用):
SELECT
"A".id AS A_id,
"B".id AS B_id,
"C".id AS C_id,
"D".id AS D_id,
"E".id AS E_id,
"F".id AS F_id,
"G".id AS G_id,
"H".id AS H_id,
"I".id AS I_id,
"Setup".id AS setup_id,
"Run".id AS run_id
FROM "A"
INNER JOIN "B" ON "A".id = "B".A_id
INNER JOIN "C" ON "B".C_id = "C".id
INNER JOIN "D" ON "C".D_id = "D".id
INNER JOIN "E" ON "D".id = "E".D_id
INNER JOIN "F" ON "E".F_id = "F".id
INNER JOIN "G" ON "F".G_id = "G".id
INNER JOIN "H" ON "G".id = "H".G_id
INNER JOIN "I" ON "H".I_id = "I".id
AND FROM
"Setup"
FULL JOIN "Run" ON "Setup".id = "Run".id
FULL JOIN "B" ON "Setup".id = "B".id;
我使用完全加入,因为每个表"运行"和"设置"仅包含一行,但与表格相关联" B"它有更多的那一行,我想把它们全部放在结果表中。
如何修复?也许尝试在select中使用select?
答案 0 :(得分:2)
您不需要完全加入即可在您描述的1对多关系中反映多行。另一个内部联接。并且没有" AND FROM"施工。您可以直接添加联接。
INNER JOIN "I" ON "H".I_id = "I".id
INNER JOIN "Setup" ON "B".id = "Setup".id;
INNER JOIN "Run" ON "Setup".id = "Run".id
您应该阅读joins。
[编辑]
注释表明数据是不对称的,因此您需要外连接,因此:
INNER JOIN "I" ON "H".I_id = "I".id
LEFT JOIN "Setup" ON "B".id = "Setup".id;
LEFT JOIN "Run" ON "Setup".id = "Run".id