我有一个表(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/