php - 如何对 MYSQL 进行多次插入,每次插入都基于子查询中一组值中的每个值?

标签 php mysql wordpress woocommerce review

我正在尝试在整个网站上传播一组评论,看来我需要为每个产品插入一次评论。因此,我有 100 个产品,我需要获取一条评论并将其插入 100 次,每个产品一次,以便该评论显示在网站上的任何位置(有点像 Etsy 的做法)。但是,我该怎么做呢?

(该系统基于woocommerce。)

我会尝试我尝试过的以下代码(所有变量/列名称都是正确的,但我很确定这不是这样的查询应该设置的方式。),但我很漂亮肯定会失败(看起来太清晰而无法正常工作)所以有人可以给我一个正确的方向插入吗?

INSERT INTO wp_comments
SET comment_author = 'britney',
comment_date = '2015-11-07 07:55:02',
comment_date_gmt = '2015-11-07 07:55:02',
comment_content= 'the comment',
comment_approved = '1',
comment_parent = '0',
user_id = '2',
post_id = t.id FOR ALL ID IN
(select ID
FROM wp_posts
WHERE post_type = 'product') as t;

最佳答案

你会想要这样做:

INSERT INTO wp_comments (
            comment_author,
            comment_date,
            comment_date_gmt,
            comment_content,
            comment_approved,
            comment_parent,
            user_id,
            post_id)
SELECT      'britney',
            '2015-11-07 07:55:02',
            '2015-11-07 07:55:02',
            'the comment',
            '1',
            '0',
            '2',
            id 
FROM        wp_posts
WHERE       post_type = 'product';

关于php - 如何对 MYSQL 进行多次插入,每次插入都基于子查询中一组值中的每个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580707/

相关文章:

php - 哪个 PHP 框架适合 RoR 开发人员?

php - 如何一次仅从一台计算机使用一个 ID/通行证登录

MySql 更新 boolean 值

mysql - PDO MySql : Can a single query find all the people that live in each region?

php - 将数据库中的数据显示到下拉菜单 CodeIgniter

mysql - SQL schema优化,如何避免union?

IE6/7 中的 CSS 问题

javascript - 如何推迟解析W3 Total Cache缓存和缩小的javascript文件的javascript

wordpress - get_avatar_url(get_avatar()) 总是返回默认的 96 大小?

php - 我可以在学说 ORM 中使用准备语句吗