mysql 查询导出(txt,csv,xls)
1 简介
工作中产品经常会临时找我导出一些数据,导出mysql查询结果数据有几种方法,下面介绍3种.
① mysql -u -p -e "sql" db > filepath
② echo "sql" | login > filepath
③ mysql login; use db; select * into outfile "filepath" from tb condition;
2 实例讲解
① mysql -uroot -p123456 -e "select * from tb_user where id = 1" testdb > ~/wbwcachedata/tb_user.txt
mysql -uroot -p123456 -e "select * from tb_user where id = 1" testdb > ~/wbwcachedata/tb_user.csv
mysql -uroot -p123456 -e "select * from tb_user where id = 1" testdb > ~/wbwcachedata/tb_user.xls
个人比较喜欢导出为csv,因为xls经常会出现几个字段合到了一列的情况.导出csv之后,以制表符tab为分隔点即可转成excel.
另外如果出现中文乱码,可直接用命令转码 iconv -futf8 -tgb2312 -otb_user.xls tb_user1.xls.或者直接用文件软件转码即可(excel转ANSI码).
② echo "select * from tb_user where id = 1" | mysql -uroot -p123456 > ~/wbwcachedata/mysqlexport0327-01.csv
错误提示: ERROR 1046 (3D000) at line 1: No database selected
没关系,我们在sql中加上use db即可:
echo "use testdb; select * from tb_user where id = 1;" | mysql -uroot -p123456 > ~/wbwcachedata/mysqlexport0327-01.csv
③ 登录mysql
mysql -uroot -p
执行sql
select * into outfile '~/wbwcachedata/mysqlexport0327-02.csv' from tb_user where id = 1;
错误提示: ERROR 1046 (3D000): No database selected
记得选择数据库
use testdb;
select * into outfile '~/wbwcachedata/mysqlexport0327-02.csv' from tb_user where id = 1;
错误提示: ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
此时有两种解决办法,第一种是改mysql的my.ini的secure-file-priv路径重启mysql.
第二种是
执行 show variables like '%secure%';
返回
+--------------------------+-----------------------+
| Variable_name | Value |
+--------------------------+-----------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | /var/lib/mysql-files/ |
+--------------------------+-----------------------+
发现mysql允许导出路径为/var/lib/mysql-files/
我们执行sql
select * into outfile '/var/lib/mysql-files/mysqlexport0327-02.csv' from tb_user where id = 1;
然后用cp/mv命令把该文件转移到我们想要的路径.
一般我是选第二种,在没必要的情况下,不应主动修改my.ini.
mysql 查询导出(txt,csv,xls)的更多相关文章
- ABAP upload file(*.txt *.csv *.xls)
转自:http://blog.csdn.net/jy00873757/article/details/8534492 在SAP我们经常会用到*.txt, *.csv, *.xls三种文件格式 *.TX ...
- 将MYSQL查询导出到文件
sql文件: set names utf8; select * from xxxxx mysql命令: mysql .sql .txt
- mysql 查询导出 excel 中文乱码 解决 --default-character-set=gbk
mysql --default-character-set=gbk -uroot -p -D open_fusion -e " select * from table1 " ...
- mysql导出csv/sql/newTable/txt的方法,mysql的导入txt/sql方法...mysql备份恢复mysqlhotcopy、二进制日志binlog、直接备份文件、备份策略、灾难恢复.....................................................
mysql备份表结构和数据 方法一. Create table new_table_nam备份到新表:MYSQL不支持: Select * Into new_table_name from old_t ...
- 将mysql的查询结果导出为csv
要将mysql的查询结果导出为csv,一般会使用php连接mysql执行查询,将返回的查询结果使用php生成csv格式再导出. 但这样比较麻烦,需要服务器安装php才可以实现. 直接使用mysql导出 ...
- mysql导出查询结果到csv方法
要将MySQL的查询结果导出为csv,一般会使用php连接mysql执行查询,将返回的查询结果使用php生成csv格式再导出. 但这样比较麻烦,需要服务器安装php才可以实现. 直接使用mysql导出 ...
- mysql导出csv/excel文件的几种方法,mysql的load导入csv数据
方法一 php教程用mysql的命令和shell select * into outfile './bestlovesky.xls' from bestlovesky where 1 order by ...
- MySQL 导入导出 CSV 文件
导入 导出 清空表中的所有数据 注意事项 常见问题 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-pri ...
- Mysql 导入导出csv 中文乱码
这篇文章介绍了Mysql 导入导出csv 中文乱码问题的解决方法,有需要的朋友可以参考一下 导入csv: load data infile '/test.csv' into table table ...
随机推荐
- 操作XML
别人已经写过很好的XML辅助类,可以直接引用后使用: 我这里自己写一个xml的操作类,目前能实现的是对一个不含集合的类可以操作,含集合的类无法将集合里的数据读取出来, 首先定义一个XML特性,用于区分 ...
- Lesson 28 No parking
Text Jasper White is one of those rare people who believes in ancient myths. He has just bought a ne ...
- harbor在谷歌云上搭建 日志
参考:https://github.com/vmware/harbor/blob/master/docs/installation_guide.md 日志: [root@instance-1 harb ...
- 【从零开始搭建自己的.NET Core Api框架】(二)搭建项目的整体架构
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- [Swift]LeetCode992. K 个不同整数的子数组 | Subarrays with K Different Integers
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A g ...
- [Swift]LeetCode1000. 合并石头的最低成本 | Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- 剖析项目多个logback配置(上)
来源:http://www.cnblogs.com/guozp/p/5949744.html 以下两个是我在使用slf4j + logback时候日志提示的问题,问题不大,都是WARN,并不真正影响运 ...
- 关于pycharm安装出现的interpreter field is empty,无法创建项目存储位置
关于pycharm安装出现的interpreter field is empty(解释器为空) 关于pycharm安装出现的interpreter field is empty,无法创建项目存储的位置 ...
- JVM基础系列第2讲:Java 虚拟机的历史
说起 Java 虚拟机,许多人就会将其与 HotSpot 虚拟机等同看待.但实际上 Java 虚拟机除了 HotSpot 之外,还有 Sun Classic VM.Exact VM.BEA JRock ...
- .NET应用程序管理服务AMS设计
AMS全称是Application Management Server即应用程序管理服:由于经常要写些一些应用服务,每次部署和维护都比较麻烦,首先要针对服务编写一个windows服务程序方便系统启动里 ...