mark_nsx Posted May 19, 2008 Share Posted May 19, 2008 Hi all, Say i have a table called `users_events` with fields `user_id` and `event_id` with the following data: user_id | event_id ------------------- 1 | 1 1 | 3 2 | 1 2 | 2 2 | 3 ------------------- What I'd like to do is merge rows with user_id = 2 into user_id = 1 and delete duplicate ones (event_id), which will return the result: user_id | event_id ------------------- 1 | 1 1 | 3 1 | 2 ------------------- Thanks in advance for the help! Cheers! Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/ Share on other sites More sharing options...
Xurion Posted May 19, 2008 Share Posted May 19, 2008 First you'll need to make all your user_id that are 2 into 1: UPDATE users_events SET user_id=1 WHERE user_id=2 Because there is no 'function' in mysql to remove duplicates, here is the simplest way to achieve it: CREATE TABLE new_users_events( user_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), event_id INT) INSERT INTO new_users_events(user_id, event_id) SELECT DISTINCT user_id, event_id FROM users_events; DROP TABLE users_events; RENAME TABLE new_users_events users_events; I can't test this from where I am, but I think this will work. Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-544680 Share on other sites More sharing options...
fenway Posted May 19, 2008 Share Posted May 19, 2008 Why not just delete the dupes? Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-544737 Share on other sites More sharing options...
Xurion Posted May 19, 2008 Share Posted May 19, 2008 I assumed he had a lot of dupes and wanted to automate. Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-544760 Share on other sites More sharing options...
mark_nsx Posted May 19, 2008 Author Share Posted May 19, 2008 once the site grows, there's a possibility of lots of dupes.. could there be a way of just combining an update, insert, and just select statements? i think there's a huge overhead in creating a new table and copying all those rows.. Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-544763 Share on other sites More sharing options...
Xurion Posted May 19, 2008 Share Posted May 19, 2008 Best way: put precautions in place to prevent dupes. Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-544834 Share on other sites More sharing options...
fenway Posted May 20, 2008 Share Posted May 20, 2008 Or use a UNIQUE index if possible. Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-545797 Share on other sites More sharing options...
mark_nsx Posted May 21, 2008 Author Share Posted May 21, 2008 the the table has precautions to prevent dupes..its just that i have a merge function which generates those dupes.. Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-546236 Share on other sites More sharing options...
Xurion Posted May 21, 2008 Share Posted May 21, 2008 Edit the function to not include dupes? Link to comment https://forums.phpfreaks.com/topic/106279-help-with-query/#findComment-546347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.