同一个表中有多个INNER JOIN

时间:2014-09-23 15:13:51

标签: mysql join

所以我有这两个表:

对象(idObjects,obj_name) 关系(idRelations,rel_name,dom_idObjects,cdom_idObjects)

dom和cdom是对象表的FK。它们代表现有对象的关系。

我正在尝试编写一个返回特定depht中的每个关系的查询,以及相应的对象名称。因此,例如,如果我希望depht为1(2个对象之间的简单关系)中的每个关系,我设法让它工作:

SELECT A.rel_name
    ,C1.obj_name AS object1_name
    ,C2.obj_name AS object2_name
FROM relations AS A
INNER JOIN Objects AS C1 ON A.dom_idObjects = C1.idObjects
INNER JOIN Objects AS C2 ON A.cdom_idObjects = C2.idObjects

然而,当试图将其扩展到2或更高的depht时,我没有得到任何结果(并且根据我的DB应该有几个):

SELECT A.rel_name
    ,C1.obj_name AS object1_name
    ,C2.obj_name AS object2_name
    ,C3.obj_name AS object3_name
    ,C4.obj_name AS object4_name
FROM relations AS A
INNER JOIN Objects AS C1 ON A.dom_idObjects = C1.idObjects
INNER JOIN Objects AS C2 ON A.cdom_idObjects = C2.idObjects
INNER JOIN Objects AS C3 ON A.dom_idObjects = C2.idObjects
    AND A.cdom_idObjects = C3.idObjects
INNER JOIN Objects AS C4 ON A.dom_idObjects = C3.idObjects
    AND A.cdom_idObjects = C4.idObjects

我想我错过了什么。是什么给了什么?

0 个答案:

没有答案