Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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 从表本身和另一个表向表中插入数据_Mysql_Database - Fatal编程技术网

Mysql 从表本身和另一个表向表中插入数据

Mysql 从表本身和另一个表向表中插入数据,mysql,database,Mysql,Database,我有两张桌子: 表1包含roleid table2包含stringtext,table1\u roleid(它是table1中roleid字段的FK) table2现在包含5个条目,每个条目都有相同的table1\u roleid值和不同的stringtext值,我想复制同一个表中的stringtext值,但要从table1中获取不同的table1\u roleid值 为了进一步解释,这里有一个陈述: table1 +--------+ | Roleid | +--------+ | 1

我有两张桌子:

表1
包含
roleid

table2
包含
stringtext
table1\u roleid
(它是table1中roleid字段的FK)

table2
现在包含5个条目,每个条目都有相同的
table1\u roleid
值和不同的
stringtext
值,我想复制同一个表中的
stringtext
值,但要从
table1
中获取不同的
table1\u roleid

为了进一步解释,这里有一个陈述:

table1
+--------+
| Roleid |
+--------+
| 1      |
| 2      |
| 3      |
+--------+

table2
+------------+---------------+
| stringtext | table1_roleid |
+------------+---------------+
| text1      |             1 |
| text2      |             1 |
| text3      |             1 |
| text4      |             1 |
| text5      |             1 |
+------------+---------------+
表2的最终结果应为:

table2
+------------+---------------+
| stringtext | table1_roleid |
+------------+---------------+
| text1      |             1 |
| text2      |             1 |
| text3      |             1 |
| text4      |             1 |
| text5      |             1 |
| text1      |             2 |
| text2      |             2 |
| text3      |             2 |
| text4      |             2 |
| text5      |             2 |
| text1      |             3 |
| text2      |             3 |
| text3      |             3 |
| text4      |             3 |
| text5      |             3 |
+------------+---------------+

我曾想过创建一个临时表,复制
表2
,每次我都可以更新临时表中的
表1\u roleid
,但我追求的是一种更智能的方法,例如,我可以放入一个循环,插入到同一个表中,而不需要临时表。

您可以通过
交叉生成所有需要的行加入
。因为结果已经包含了表2中的一些行,所以需要将它们过滤掉。这里有一个方法:

insert into table2(string_text, table1_roleid)
    select t2.string_text, t1.roleid
    from table2 t2 cross join
         table1 t1
    where t1.roleid <> 1;

另一种方法是在两个表之间创建笛卡尔平面。与交叉连接相同

Insert into table2 (string_text, table1_roleid)
  select t2.stringtext, t1.Roleid 
    from table1 t1,
         table2 t2
   where t1.Roleid <> 1
插入到表2中(字符串文本,表1角色ID)
选择t2.stringtext,t1.Roleid
从表1 t1中,
表2 t2
其中t1.roleid1
请看这里:


它所做的是合并指定表中的每个注册表,除了(或仅限于)那些符合条件的注册表。

我不知道交叉连接。谢谢
Insert into table2 (string_text, table1_roleid)
  select t2.stringtext, t1.Roleid 
    from table1 t1,
         table2 t2
   where t1.Roleid <> 1