我的 MySQL 数据库中有 2 个表。
我需要将两个表输出到 SQL 转储文件。(创建脚本)
但是,我可能只会转储某些行。(例如,where column = value)。
第一个表没问题。
但是,第二个表可能只会转储行,例如列 = 第一个结果集中存在的值。
我想使用 mysqldump,但我不认为它适用于这种类型的查询,除非我进行某种连接或联合?
我还可以使用 PHP 并在循环遍历行的同时创建文件,但我认为这不是最有效的方法。
任何建议或帮助将不胜感激!
最佳答案
谢谢@MarcB为了回答这个问题,它 100% 有效。我发布这个是为了遵循堆栈溢出的回答规则,同时也是为了展示我最终使用的内容。
所以这是我在 bash 脚本中运行的 2 个 mysqldump 命令:(我用“x”替换了一些私有(private)数据)
#!/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
关于php - mysql 从相互关联的特定表中转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440953/