mysql - Insert duplicate rows for a new id based on result of another query -


so i'll best describe query i'm trying build.

i have table i'll call user_records has data (several rows) relational id, userid (from table users). each row, need duplicate each row user. know run this:

insert user_records (userid, column1, column2, ...)   select 10 userid, column1, column2...   user_records  userid = 1   

this copy existing rows userid 1 userid 10.

but want run userids active , don't exists in table. want execute query first:

select userid   users   users.active = 1   , not exists (   select * user_records  users.userid = user_records.userid) 

using joins or combining 2 queries, can run query , replace 10 in former query duplicates rows series of userids?

thanks in advance!

one way create cross join:

insert user_records (userid, column1) select u.userid, ur.column1 user_records ur   cross join users u  ur.userid = 1   , u.active = 1   , u.userid not in (select userid user_records); 

sql fiddle demo

this insert new rows user_records each userid doesn't exist in table, copying data userid 1.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -