mysql - SQL 复制和更新表

标签 mysql sql

我有一个名为 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/

相关文章:

java - 时间变化会影响 Java 日期比较功能吗?

sql - 交叉表查询提供了意外的结果

mysql - 选择用户不是特定用户的 friend

php - 在 PHP + mysql 和 HTML 表中需要一些不合逻辑的帮助

php:将特殊字符插入myadmin数据库

MySQL 插入不正确的十进制值

sql - 优化 SQL 连接单列与另一个表中的多列

sql - 替换存储在 SQL Server 数据库列中的 XML 中的节点名称

mysql - 从R中的mysql(使用for循环(?))选择数据

mysql - 无法销毁 Rails 中某些模型的记录