I'm working on removing duplicates from a legacy database for a client of mine, and I've found a MySQL query to do just that. I want to create a Rake task to run the query for the production server. How would I do that?
MySQL query:
select * from community_event_users;
create table dups as
select distinct username, count(*)
from community_event_users group by username
having count(*) > 1;
delete community_event_users from community_event_users inner join dups
on community_event_users.username = dups.username;
insert into community_event_users select username from dups;
You can execute a MySQL query towards a given database by opening the database with phpMyAdmin and then clicking on the SQL tab. A new page will load, where you can provide the desired query. When ready click on Go to perform the execution. The page will refresh and you will see the results from the query you provided.
To do that, first select the desired database from the left column menu by double-clicking it. Then type in the MySQL query you want to run in the text field in the middle of the program window and use the yellow lightning button above that text field to run the query.
If you are on Rails and using ActiveRecord you can simply use:
ActiveRecord::Base.execute(my_sql)
ActiveRecord::Base.connection.execute(my_sql)
Where my_sql is your SQL string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With