mysql - Insert missing records from table -


i have 2 tables users (id, username, etc...) , user_contacts (id, userid, contactid, etc...).

given have user w/ id of 84, efficient query in order have missing records inserted user_contacts associate user 84 w/ other users?

given recent comment, should work:

insert user_contacts (userid, contactid) select u.id, 84 users u     left join user_contacts uc on u.id = uc.userid , uc.contactid = 84 uc.id null 

this insert row user_contacts table each user don't have row contactid 84. sure specify columns correctly.

alternatively, use not in or not exists. example,

insert user_contacts (userid, contactid) select u.id, 84 users u u.id not in (     select userid      user_contacts      contactid = 84) 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -