我有一个 childTable 和 parentTable,familyTable 是我新创建的表,我试图这样做以链接子表和父表的唯一 ID
我的预期结果如下表所示
家庭表
-------------
FamilyID | ParentID, ChildID
------------
1 | 1 | 1
2 | 2 | 2
但我没能做到,下面是我当前的数据库设计
** 当前数据库设计**
子表
-------------
ChildID | ChildNm
------------
1 | A
2 | B
父表
-------------
ParentID| ParentNm
------------
1 | A
2 | B
家庭表
-------------
FamilyID | ParentID, ChildID
------------
1 |
2 |
下面是我的插入语句
INSERT INTO FamilyTable (ParentID,ChildID)
SELECT p.ParentID, c.ChildID
FROM ParentTable p, ChildTable c
WHERE NOT EXISTS (SELECT 1 FROM FamilyTable where parentsID=p.parentID and childID=c.childID)
我对此查询的实际结果
家庭表
-------------
FamilyID | ParentID, ChildID
------------
1 | 1 | 1
2 | 2 | 1
3 | 1 | 2
4 | 2 | 2
这不是我想要的结果,任何人都可以给我一些想法吗?