【问题标题】:mysql dump from specific tables that relate to each othermysql 从相互关联的特定表中转储
【发布时间】:2015-08-07 02:07:48
【问题描述】:

我的 MySQL 数据库中有 2 个表。

我需要将两个表都输出到 SQL 转储文件。(创建脚本)

但是,我只能转储某些行。(例如 where column = value)。

这对于第一个表来说很好。

但是,第二个表可能只转储行,例如column = 存在于第一个结果集中的值。

我想使用 mysqldump,但我认为它不适用于这种类型的查询,除非我进行某种连接或联合?

我也可以使用 PHP 并在遍历行时创建文件,但我认为这不是最有效的方式。

任何建议或帮助将不胜感激!

【问题讨论】:

  • 您可以在 mysqldump 中指定 where 子句,但如果您的两个表的子句不同,则必须进行两次单独的转储。 mysqldump db1 table1 --where ... > dump.sql ; mysqldump db2 table2 --where .. >> dump.sql
  • @MarcB 这很好,但是,第二个 where 子句依赖于第一个。那是我的实际问题...
  • there --where 子句可以包含子选择:dba.stackexchange.com/questions/4654/…
  • @MarcB,谢谢,这正是我想要的!

标签: php mysql mysqldump


【解决方案1】:

感谢 @MarcB 回答这个问题,它的工作效率是 100%。我发布这个是为了遵循堆栈溢出规则的回答,并展示我最终使用的内容。

这是我在 bash 脚本中运行的 2 个 mysqldump 命令:(我用“x”替换了一些私有数据)

#!/bin/bash

#dump the first table
mysqldump -h xxx.xxx.xxx.xxx -u xxxxx -pxxxxx --where="name='value'" databasename table1 > /home/xxxxx/xxxxx/table1.sql

#dump the second table that relates to the first one
mysqldump -h xxx.xxx.xxx.xxx -u xxxxx -pxxxxx --lock-all-tables --where="field_id in (select anotherfield_id from table1 where name='value')" databasename table2 > /home/xxxxx/xxxxx/table2.sql

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    相关资源
    最近更新 更多