如何执行一个SQL文件,并将结果保存到一个命令中的txt文件中? [英] How can I execute an SQL file, and save the results to a txt file in one command?

查看:364
本文介绍了如何执行一个SQL文件,并将结果保存到一个命令中的txt文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ubuntu终端.我找到了有关运行.sql文件以及如何将查询结果保存到外部文件的答案.我如何同时做他们两个?

I am using an Ubuntu terminal. I've found answers about running .sql files, and how to save query results to an external file. How can I do them both at the same time?

我已经尝试过以下命令:source /path/to/file.sql into outfile /path/to/outfile.txt.

I've tried this command: source /path/to/file.sql into outfile /path/to/outfile.txt.

我尝试将into outfile /path/to/outfile.txt直接添加到file.sql并运行source /path/to/outfile.sql.

I've tried to add into outfile /path/to/outfile.txt directly into file.sql and running source /path/to/outfile.sql.

我已经尝试过mysql -u <username> -p <database> file.sql > results.txt.如果使用类似-e "select * from myTable"的方式切换file.sql,那么它可以正常工作.

I've tried mysql -u <username> -p <database> file.sql > results.txt. If I switch file.sql with something like this -e "select * from myTable", then it works fine.

我该怎么做?

这是file.sql

select myID from Players where score > 80;

这是一个简单的查询,但是如果我能弄清楚这一点,我可以尝试进行更大的查询.

It's a simple query, but if I can figure this thing out, I can try to do bigger queries.

推荐答案

您的答案在这里: http://dev.mysql.com/doc/refman/5.7/en/mysql.html

您可以像这样在脚本文件(批处理文件)中执行SQL语句:

You can execute SQL statements in a script file (batch file) like this:

shell> mysql db_name< script.sql> output.tab

shell> mysql db_name < script.sql > output.tab

尝试一下:mysql -u username -p database < file.sql > results.txt

这篇关于如何执行一个SQL文件,并将结果保存到一个命令中的txt文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆