如何从 rake 任务运行 MySQL 查询?
我正在为我的客户从旧数据库中删除重复项,并且我找到了一个 MySQL 查询来做到这一点。我想创建一个 Rake 任务来运行生产服务器的查询。我该怎么做呢?
MySQL 查询:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Rails 并使用 ActiveRecord,您可以简单地使用:
其中 my_sql 是您的 SQL 字符串。
If you are on Rails and using ActiveRecord you can simply use:
Where my_sql is your SQL string.
对于 Rails 5 及以上版本,你最好使用:
For Rails 5 and above you better use: