在我的数据库中有一个表有3个FK到其他3个表我希望选择此表中的数据+其他3个表中的数据当FK值=其他表中的PK值时
答案 0 :(得分:3)
SELECT *
FROM t1
JOIN t2
ON t2.id = t1.t2_id
JOIN t3
ON t3.id = t1.t3_id
JOIN t4
ON t4.id = t1.t4_id
答案 1 :(得分:2)
SELECT t1.Field1, t2.Field2, t3.Field3, t4.Field4
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.ID
INNER JOIN Table3 t3 ON t1.ID = t3.ID
INNER JOIN Table4 t4 ON t1.ID = t4.ID
如果表2 - 4中的行总是不匹配,并且您希望这些记录仍然返回,则将INNER JOIN更改为LEFT JOIN
答案 2 :(得分:0)
SELECT * FROM ParentTable as pt
LEFT JOIN table1 as t1
ON pt.t1_ID = t1.ID
LEFT JOIN table2 as t2
ON pt.t2_ID= t2.ID
LEFT JOIN table3 as t3
ON pt.t3_ID= t3.ID
即使表1,2或3中没有匹配项,也会返回记录。@ Ada和@Q将排除这些记录。
答案 3 :(得分:0)
SELECT * FROM TBL1 T1 RIGHT JOIN TBL2 T2 ON T1.id = T2.id RGIHT加入TBL3 T3 ON T3.id = T2.id
这将返回Right表中的所有记录以及Left表中的匹配记录。