mysql - 转储具有限制和所有关联的一个数据库表的最佳方法

标签 mysql ruby-on-rails yaml mysqldump dump

class Profile < ActiveRecord::Base
  has_many :favorites, :dependent => :destroy
  has_many :friends, :dependent => :destroy
end

我需要这样的东西:

mysqldump --opt --where="1 limit 1000" -uroot development profiles  > profiles.sql

但是这个转储包含(正如预期的那样)只有 1000 个配置文件行,没有关联 friend 、收藏夹。

我应该使用 YAML 来做还是应该怎么做?

最佳答案

获取前 5000 条记录:

mysqldump --opt --where="1 limit 5000" -uroot development profiles  > profiles.sql 

然后查找所有与这条记录关联的好友:

mysqldump --opt --lock-all-tables --where="profile_id IN (SELECT * FROM (SELECT id FROM profiles LIMIT 5000) temp);" -uroot development friends  > friends.sql

关于mysql - 转储具有限制和所有关联的一个数据库表的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13356041/

相关文章:

amazon-web-services - 如何在 CloudWatch 控制面板模板中使用参数

PHP: "->"无法处理 mysql 结果集

php - Mysql在指定时间范围内按用户ID获取最后更新

mysql - SQL : WHERE with variable?

php - 如果 mySQL 行为空,if else 语句不返回任何值?

python - 如何使用 PyQt4 GUI 编辑 yaml 文件

docker-compose - 如何使用 yq 删除 YAML 文件中的属性?

ruby-on-rails - unicorn 和 nginx 的奇怪问题导致 502 错误

ruby-on-rails - 在回调中访问验证上下文

ruby-on-rails - 基于 Rails 的 S3 文件管理器