将Mysql查询结果写入到文件

来源:互联网 发布:js 对象赋值 编辑:程序博客网 时间:2024/05/17 21:58
方法1:
shell> mysql -uroot -proot -h localhost xxx库 -e " select * from xxx表 limit 1" >> out.txt
 
方法2:
#>echo "select * from xxx表 limit 1" | /usr/local/mysql/bin/mysql -h 127.0.0.1  -uroot -proot xxx库 > /home/out.txt
 
方法3:
mysql>select * from xxx表 INTO OUTFILE './xxx.txt';
 
前两种都是在系统命令行下执行

方法3需要登录到mysql并use数据库,生成的文件只能放在mysql的数据目录中,Mysql的数据目录就是存放mysql的表的目录 **/mysql/var


方法4:

在shell命令行中利用批处理操作,先把sql写入到文件中,然后执行

shell> mysql < batch-file > mysql.out

如果mysql需要加入参数则:

shell> mysql -hxxx -pxxx -Pxxxx < batch-file > mysql.out