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

时间:2012-03-13 22:59:14

标签: 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。

我怎样才能做到这一点?

1 个答案:

答案 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