[英]Copy Data from one column to another column in a different table using MySQL
我正在嘗試使用MySQL將數據從一列復制到另一張表中的另一列中,但是我要導入的表具有外鍵約束,這使我無法做到這一點;
這是我想從(product_code)列中導入的表
表格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 | | |
+----+--------------+-------------+-------+--------------+-----------+---------+-------+-------+
我想將product_code
列復制到sku
列
表2
+----------+----------+-------+--------------+
| id | value_id | pid | sku |
+----------+----------+-------+--------------+
| 20315857 | 369781 | 41257 | 001 |
| 20315858 | 369782 | 41256 | Config - ST5 |
+----------+----------+-------+--------------+
問題是,表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
表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 |
+----------+-----------+-------------+----------------+
這是我的查詢
INSERT INTO table2 (sku) SELECT product_code FROM table1;
table1 product_code
列中包含超過200萬條記錄,並導致我的服務器在查詢期間崩潰。
我知道一定有更好的方法可以做到這一點,但我不知道該如何尋求幫助,如果可能的話……?
對我來說,您的查詢看起來還可以。
嘗試在where子句中使用一些ID或在select中使用limit將“插入-選擇”分成多個部分。
插入100條記錄。 看看進展如何。 檢查您的time_out變量。 如果需要,相應增加。
您試圖在table_1
插入許多不同的列,但沒有從第一個表中選擇這些列。 因此,當您應該插入4時,您將插入一列。我認為您正在尋找的是:
INSERT INTO table_1(name) SELECT firstname FROM table_2;
如果要為其他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;
只需將default_
id替換為您自己的默認值即可,無論它們是什么。
解決方法很簡單,
只需將查詢包裝在里面:
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;
數據輸入后,唯一的困難是數據集的大小,但將其分成多個塊很有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.