edelwater
edelwater

Reputation: 2802

SQL insert a foreach select on same table?

Can I, instead of doing this via PHP, combine this in 1 sql statement?

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);";
}

(for WordPress: for every post that has a category (14) add a new term relationship (1597))

Upvotes: 1

Views: 1155

Answers (2)

dkretz
dkretz

Reputation: 37655

Better something like:

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

Upvotes: 1

Mark Brackett
Mark Brackett

Reputation: 85665

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

Upvotes: 4

Related Questions