mysql - SQL 查询将字段值复制到同一个表中

标签 mysql sql

我有一个表(post_order),结构如下:

   | post_id | term_id | post_type | taxonomy |
   |    1    |    6    |    post   | category |
   |    2    |    6    |    post   | category |
   |    3    |    6    |    post   | category |

我想将所有字段复制到同一个表中,但更改 term_id(例如 7),因此表最终将如下所示:

   | post_id | term_id | post_type | taxonomy |
   |    1    |    6    |    post   | category |
   |    2    |    6    |    post   | category |
   |    3    |    6    |    post   | category |
   |    1    |    7    |    post   | category |
   |    2    |    7    |    post   | category |
   |    3    |    7    |    post   | category |

我知道我可以做这样的事情:

INSERT INTO post_order (post_id, term_id, post_type, taxonomy)
SELECT post_id, term_id, post_type, taxonomy
FROM post_order
WHERE term_id = 6

但这会产生完全相同的重复,并且不会更改 term_id。

我怎样才能做到这一点?

最佳答案

INSERT 语句中将 term_id 硬编码为 7:

INSERT INTO post_order (post_id, term_id, post_type, taxonomy)
SELECT post_id, 7, post_type, taxonomy
FROM post_order
WHERE term_id = 6

关于mysql - SQL 查询将字段值复制到同一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9693503/

相关文章:

python - pyodbc - 插入 MySQL 不起作用

c# - 在Mysql数据库中插入Textbox.text "123456"作为密码

mysql - 使用更新触发器将数据从一个表移动到另一个表

php - 登录脚本PHP Mysql中的错误

mysql - SQL 查询。连接但在一张表中,每个元组出现 2 次连接属性

sql - 将前一行值减去当前行

php - mysqli-准备语句失败,错误为 "no table used"

mysql - Arch linux 安装mysql access denied报错

sql - 具有子查询和值的SQL INSERT INTO

mysql - mysql命令耗时太长,有什么简化的吗?