我有一个名为 oc_product_to_category 的 SQL 表
sql表示例。
product_id category_id
1 146
2 146
.. 146
16 147
17 147
... ...
1800 191
所以我的问题是如何复制表格并更改并仅更改category_id
?
所以我的结果需要是这样的。 sql表嵌套结果示例
product_id category_id
1 146
1 200
2 146
2 200
.. 146
.. 200
16 147
16 260
17 147
17 260
... ...
1800 191
1800 291
抱歉,解释不好,但我无法导入图像。
插入 oc_product_to_category (category_id) 从 oc_product_to_category 中选择“147”,其中类别_id = 146
我像这样使用 somtheihng,但出现错误 Duplicate enrty 147 - 0 for key PRIMARY
最佳答案
使用以下命令创建包含表索引的精确副本:
CREATE TABLE new_table LIKE old_table;
然后插入
INSERT INTO new_table
SELECT whatever_the_logic_is_for_your_new_data FROM old_table;
如果你不想包含索引和外键约束等,你也可以这样做:
CREATE TABLE new_table AS
SELECT whatever_the_logic_is_for_your_new_data FROM old_table;
关于mysql - SQL 复制和更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25909947/