Mysql 如何为数据库中的每个表转储最后10行?

Mysql 如何为数据库中的每个表转储最后10行?,mysql,database,export,mysqldump,database-backups,Mysql,Database,Export,Mysqldump,Database Backups,有很多关于如何从数据库表中转储最后的“n”行的文章。例如,mysqldump--user=superman--password=batman--host=gothamcity.rds.com--where=“1=1按id排序限制10”DB\u NAME TABLE\u NAME./path/to/dump/file.sql,如和中的这些答案所示 但是,如何告诉mysqldump为数据库中的每个表导出最后的“n”行呢。其基本思想是获取所有表名的列表,然后将该列表导入bash中的while循环,其中

有很多关于如何从数据库表中转储最后的“n”行的文章。例如,
mysqldump--user=superman--password=batman--host=gothamcity.rds.com--where=“1=1按id排序限制10”DB\u NAME TABLE\u NAME./path/to/dump/file.sql
,如和中的这些答案所示


但是,如何告诉mysqldump为数据库中的每个表导出最后的“n”行呢。其基本思想是获取所有表名的列表,然后将该列表导入bash中的while循环,其中每个表分别转储到一个单独的转储文件(以表名命名)

mysql--user=superman--password=batman--host=gothamcity.rds.com--port=3306--database=jokersDB--execute=“show tables”--静默--batch |同时读取tablename;do mysqldump--user=superman--password=batman--host=gothamcity.rds.com--port=3306--where=“1=1按id排序限制10”jokersDB$tablename--add drop table>$tablename.sql;完成

成功了。唯一的问题是,它将每个表转储到它自己的SQL文件中——并不是所有表都转储到一个文件中。但是我想这些单独文件的内容也可以通过其他一些bash命令连接到一个文件中。

您可以对多个表使用
--where
标志,只要它在语法上对每个表都有意义。因此,如果您的所有表都有一个名为
id
的代理PK列,那么您根本不需要命名任何表。只需使用
--all databases
标志(或指定所需的数据库名称)和
--where
标志以及指定的ORDER BY/LIMIT进行转储即可

mysqldump--user=superman--password=batman--host=gothamcity.rds.com--where=“1=1按id排序DESC LIMIT 10”--数据库DB_NAME>/path/to/dump/file.sql

增加

如果我们把mysqldump的编写部分改成这样

>$tablename.sql

进入

>>jokersDB.sql

我们可以在单个文件中转储偏移量为10的所有数据库数据

完全命令:

mysql--user=superman--password=batman--host=gothamcity.rds.com--port=3306--database=jokersDB--execute=“show tables”--silent--batch |同时读取tablename;do mysqldump--user=superman--password=batman--host=gothamcity.rds.com--port=3306--where=“1=1按id排序限制10”jokersDB$tablename--add drop table>jokersDB.sql;完成

jokersDB.sql文件必须存在且为空