Contributor
Kyle
February 9
Edit

MySQL - Merge two user groups users into one current group, and delete the second group.

Merge two user groups users into one current group, and delete the second group.
Note: some users might be in both groups already.

# Show users in groupID 111 are also in groupID 222

SELECT user_id FROM user_group 
WHERE user_group_id={111} AND user_id IN (SELECT user_id FROM user_group WHERE user_group_id={222} ) 

# Change user_group_id 222 to 111. Use 'UPDATE IGNORE'  assume user_id & user_group_id are unique index

UPDATE IGNORE user_group  SET user_group_id={111}
WHERE user_group_id={222} AND user_id NOT IN (SELECT user_id FROM user_group WHERE user_group_id={222} ) 

# option: delete remaining deplucated user_ids in user_group_id 222 if you don't want user_group 222 anymore.

Promotional / Featured products: Contents Settings Layouts
Experteer's Scripting: Coding - Experts Only
Send us a message. We will reply as soon as we can.
×

Save your professional blogger profile.

Join our blogger group and earn more income.

Sign up Log in