INSERT on each id from SELECT

I just added users into my WordPress installation from another CMS, and now I need for each user id in wp_users insert new value with its id into wp_usermeta. Is there any ideas for this in SQL?

Related posts

Leave a Reply

3 comments

  1. You can use a SELECT statement as a source of values for an INSERT, like this:

    INSERT IGNORE INTO wp_usermeta (id)
    SELECT u.id FROM wp_users u
    

    The IGNORE keyword tells MySQL to insert the rows that it can, and if any rows we try to insert are “duplicate” rows (throw an exception), then just let those rows not be inserted, and ignore the errors that would otherwise be thrown.

  2. Making use of single-quoted string literals, you can use the INSERT INTO ... SELECT syntax to simultaneously select all user ids from wp_users and insert string values into wp_usermeta:

    INSERT INTO wp_usermeta (user_id, meta_key, meta_value) 
      SELECT
        wp_users.ID, 
       /* Single-quoted string literals for meta key and value... */
       /* Substitute the key and value you want to insert */
       'new_meta_keyname', 
       'new_meta_value' 
      FROM wp_users