SQL在同一个表上插入一个foreach选择?

时间:2011-06-04 02:23:35

标签: sql select insert

我可以通过PHP将其与1 sql语句结合使用吗?

object_ids = "select object_id from `wp_term_relationships` where `term_taxonomy_id` = 14;";
foreach(object_ids as object_id) 
{
    "insert into `wp_term_relationships` VALUES (" . object_id . ",1597,0);";
}

(对于WordPress:对于每个有类别(14)的帖子添加新的术语关系(1597))

2 个答案:

答案 0 :(得分:4)

INSERT INTO `wp_term_relationships`
SELECT object_id, 1597, 0
FROM `wp_term_relationships`
WHERE `term_taxonomy_id` = 14;

答案 1 :(得分:1)

更好的是:

 INSERT INTO wp_term_relationships
 SELECT  object_id, 1597, 0
 FROM wp_term_relationships
 WHERE term_taxonomy_id = 14