如何将 sql 查询的结果转储到文件中

How to dump result of a sql query into a file

好的,所以我现在需要将 sql 查询的结果转储到一个文件中,这是为了备份目的,我从我的终端尝试了 运行 以下内容:

mysql -e "select * from products where brand_id=5" -u root -p database_name > dumpfile.sql

这会转储数据,但使用 "insert into statements",所以如果稍后我想将此数据转储回我的数据库,我将重新组合它。这不是正确的方法。所以请建议我如何转储查询结果以供备份?

以下命令行(使用 --where 选项)应该可以工作:

mysqldump -u root -p database_name products --where="brand_id=5" > dumpfile.sql

有关 mysqldump 命令的更多信息:https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html

使用mysqldump

mysqldump -u root -p database_name products --where "brand_id=5"> dump.sql

更多阅读: http://mechanics.flite.com/blog/2012/11/15/3-methods-to-extract-a-subset-of-your-data-using-mysqldump/