[英]Copy Data from one column to another column in a different table using MySQL
I am trying to copy data from one column to another column in a different table using MySQL but the table that I am importing into has a foreign key restraint that is preventing me from doing this; 我正在尝试使用MySQL将数据从一列复制到另一张表中的另一列中,但是我要导入的表具有外键约束,这使我无法做到这一点;
Here is the table that I would like to import from (product_code) column 这是我想从(product_code)列中导入的表
Table1 表格1
+----+--------------+-------------+-------+--------------+-----------+---------+-------+-------+
| id | product_code | distributor | brand | productname | wheelsize | pcd_1 | pcd_2 | pcd_3 |
+----+--------------+-------------+-------+--------------+-----------+---------+-------+-------+
| 1 | F7050MHS20A2 | ******* | MAK | MOHAVE | 7 x 15 | 5x139.7 | | |
| 2 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 3 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 4 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 5 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 6 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 7 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 8 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 9 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
| 10 | 3480 | ******* | KFZ | Winter Steel | 4.5 x 13 | 3x98 | | |
+----+--------------+-------------+-------+--------------+-----------+---------+-------+-------+
I would like to copy the product_code
column into the sku
column 我想将
product_code
列复制到sku
列
Table2 表2
+----------+----------+-------+--------------+
| id | value_id | pid | sku |
+----------+----------+-------+--------------+
| 20315857 | 369781 | 41257 | 001 |
| 20315858 | 369782 | 41256 | Config - ST5 |
+----------+----------+-------+--------------+
The problem is that the value_id
column in Table2 is referencing value_id
Table3 so I either get a Foreign key restraint error
or lock wait timeout
问题是,表2中的
value_id
列正在引用value_id
因此我得到了外key restraint error
或lock wait timeout
a foreign key constraint fails (`gravytra_topgear`.`am_finder_map`, CONSTRAINT `FK_MAP_VALUE` FOREIGN KEY (`value_id`) REFERENCES `am_finder_value` (`value_id`) ON D
Table 3 表3
+----------+-----------+-------------+----------------+
| value_id | parent_id | dropdown_id | name |
+----------+-----------+-------------+----------------+
| 6771 | 0 | 4 | AC |
| 6749 | 0 | 4 | Acura USA |
| 6895 | 0 | 4 | Aixam |
| 6872 | 0 | 4 | Alfa Romeo |
| 6853 | 0 | 4 | Alfa Romeo USA |
| 6772 | 0 | 4 | Alpina |
| 6815 | 0 | 4 | AMC USA |
| 6854 | 0 | 4 | Anhui Anchi |
| 6928 | 0 | 4 | Ariel |
| 6783 | 0 | 4 | ARO |
+----------+-----------+-------------+----------------+
Here is my Query 这是我的查询
INSERT INTO table2 (sku) SELECT product_code FROM table1;
The table1 product_code
column has over 2million records inside it and has caused my server to crash during the query. table1
product_code
列中包含超过200万条记录,并导致我的服务器在查询期间崩溃。
I know that there must be a better way to do this but I can't figure out how to so would like some assistance if possible please...? 我知道一定有更好的方法可以做到这一点,但我不知道该如何寻求帮助,如果可能的话……?
To me your query looks OK. 对我来说,您的查询看起来还可以。
try breaking the "insert - select" into parts either using some id in the where clause or using limit in the select. 尝试在where子句中使用一些ID或在select中使用limit将“插入-选择”分成多个部分。
insert 100 records. 插入100条记录。 See how it goes.
看看进展如何。 check your time_out variables.
检查您的time_out变量。 increase accordingly if needed.
如果需要,相应增加。
You are trying to insert lots of different columns into table_1
but you aren't selecting those out of the first table. 您试图在
table_1
插入许多不同的列,但没有从第一个表中选择这些列。 So you're inserting one column when you should be inserting 4. I think what you're looking for is: 因此,当您应该插入4时,您将插入一列。我认为您正在寻找的是:
INSERT INTO table_1(name) SELECT firstname FROM table_2;
If you want to have default values for the other 3 columns, you could easily put them in the select query as well: 如果要为其他3列提供默认值,则也可以轻松地将它们放入select查询中:
INSERT INTO table_1(value_id, parent_id, dropdown_id, name)
SELECT default_id, default_parent_id, default_dropdown_id, firstname
FROM table_2;
Simply replace the default_
ids with your own defaults, whatever they may be. 只需将
default_
id替换为您自己的默认值即可,无论它们是什么。
The fix was simple, 解决方法很简单,
Just wrap the query inside: 只需将查询包装在里面:
SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS = 0;
SET UNIQUE_CHECKS = 0;
# YOUR QUERIES HERE...
SET AUTOCOMMIT = 1;
SET FOREIGN_KEY_CHECKS = 1;
SET UNIQUE_CHECKS = 1;
The the data goes in, only difficulty is the large size of the dataset but breaking it into chunks helped alot. 数据输入后,唯一的困难是数据集的大小,但将其分成多个块很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.