Search code examples
phpmysqlwordpresswoocommercereview

How to do multiple inserts into MYSQL, each insert based on each value in a set of values from a subquery?


I'm trying to propogate a set of review across the entire site, and it appears I need to insert a review once for every product. So, I have 100 products, and I need to take one review and insert it 100 times, once for every product, so that the review shows up everywhere on the site (Kind of like it does for Etsy.). But, how do I do this?

(This system is based on woocommerce.)

I would try the following code I've attempted (All the variable/column names are right, but I'm pretty sure this is not the way such a query is supposed to be setup.), but I'm pretty sure it'll fail(Seems too legible to be functional) so could someone give me a nudge in the right direction?

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;

Solution

  • You'll want to do this:

    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';