Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从2个不同的表插入语句_Mysql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Mysql 从2个不同的表插入语句

Mysql 从2个不同的表插入语句,mysql,sql-server,sql-server-2008,tsql,Mysql,Sql Server,Sql Server 2008,Tsql,我有一个childTable和parentTable,familyTable是我新创建的表 我试图做什么来链接子表和父表的唯一ID 我的预期结果如下表所示 家庭表 ------------- FamilyID | ParentID, ChildID ------------ 1 | 1 | 1 2 | 2 | 2 ------------- ChildID | ChildNm ------------ 1 | A 2 | B

我有一个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       |  
   -------------
FamilyID | ParentID, ChildID
------------
1       |  1  |  1
2       |  2  |  1
3       |  1  |  2
4       |  2  |  2
但我没有做到这一点,下面是我目前的数据库设计

**当前数据库设计**

ChildTable

   -------------
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       |  
   -------------
FamilyID | ParentID, ChildID
------------
1       |  1  |  1
2       |  2  |  1
3       |  1  |  2
4       |  2  |  2
父表

   -------------
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       |  
   -------------
FamilyID | ParentID, ChildID
------------
1       |  1  |  1
2       |  2  |  1
3       |  1  |  2
4       |  2  |  2
家庭表

   -------------
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       |  
   -------------
FamilyID | ParentID, ChildID
------------
1       |  1  |  1
2       |  2  |  1
3       |  1  |  2
4       |  2  |  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   | 2
-------------
ChildID | ChildNm
------------
1       |  A
2       |  B
   -------------
ParentID| ParentNm
------------
1       |  A
2       |  B
   -------------
FamilyID | ParentID, ChildID
------------
1       |  
2       |  
   -------------
FamilyID | ParentID, ChildID
------------
1       |  1  |  1
2       |  2  |  1
3       |  1  |  2
4       |  2  |  2

这并不是我想要的结果,任何人都可以给我一些想法???

假设你在parentnm/childnm上匹配它们(否则你只需要交叉连接就可以得到你目前拥有的所有可能的组合)


不清楚的。考虑提供一个SqLoFDLE和你想要的结果集。你怎么知道父母和孩子之间的关系?基本上我需要做一个数据修补,在原来的设计中没有父母表和家庭表,所以要求插入一个带有ID ID的父ID的列表。