我有一个表格(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。
我怎样才能做到这一点?
答案 0 :(得分:3)
在term_id
声明中对INSERT
到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