【问题标题】:SQL insert a foreach select on same table?SQL 在同一张表上插入一个 foreach 选择?
【发布时间】:2011-06-04 02:23:35
【问题描述】:

我可以不通过 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))

【问题讨论】:

    标签: sql select insert


    【解决方案1】:
    INSERT INTO `wp_term_relationships`
    SELECT object_id, 1597, 0
    FROM `wp_term_relationships`
    WHERE `term_taxonomy_id` = 14;
    

    【讨论】:

      【解决方案2】:

      更好的是:

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

      【讨论】:

        猜你喜欢
        • 2011-04-03
        • 2021-02-17
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        • 2018-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多