Insert New Row in Table 3 if combination of Col A and Col B in Table C Don't Exist

a 夏天 提交于 2019-12-06 07:58:29

If you want to avoid duplicates, then add a unique index or constraint:

create unique index idx_mdl_groups_members_2 on mdl_groups_members(groupid, userid)

Then use on duplicate key update:

INSERT INTO mdl_groups_members (groupid, userid)
    SELECT l.mgroup AS moodle, r.id AS mdl_user  
    FROM moodle l JOIN
         mdl_user r   
         ON l.orders_id = r.id
    WHERE l.mgroup > 0
    ON DUPLICATE KEY UPDATE groupid = VALUES(groupid);

The on duplicate key update causes the insert to ignore duplication errors, inserting other rows with no problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!