如何从 rake 任务运行 MySQL 查询?

发布于 2024-11-25 00:39:04 字数 514 浏览 0 评论 0原文

我正在为我的客户从旧数据库中删除重复项,并且我找到了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-12-02 00:39:04

如果您使用 Rails 并使用 ActiveRecord,您可以简单地使用:

ActiveRecord::Base.execute(my_sql)

ActiveRecord::Base.connection.execute(my_sql)

其中 my_sql 是您的 SQL 字符串。

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.

甜妞爱困 2024-12-02 00:39:04

对于 Rails 5 及以上版本,你最好使用:

ApplicationRecord.connection.execute <<~END_OF_SQL
  YOUR QUERY HERE
END_OF_SQL

For Rails 5 and above you better use:

ApplicationRecord.connection.execute <<~END_OF_SQL
  YOUR QUERY HERE
END_OF_SQL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文