热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将mysql查询结果转换为CSV(带有复制/粘贴)-ConvertmysqlqueryresultstoCSV(withcopy/paste)

Ioftenworkincommandlinemysql.Acommonneedistotakeaquerysresultsandimporttheminto

I often work in command line mysql. A common need is to take a query's results and import them into a Numbers document (similar to an Excel document).

我经常在命令行mysql中工作。一个常见的需求是获取查询的结果并将其导入数字文档(类似于Excel文档)。

What is the fastest method for doing this?

最快的方法是什么?

Method 1: Select into outfile

You can select into an outfile directly from MySQL, but this takes several steps.

您可以直接从MySQL选择输出文件,但这需要几个步骤。

  1. export your query with all the necessary arguments to make it a CSV format, like FIELDS OPTIONALY ENCLOSED BY and DELIMITED BY.
  2. 导出包含所有必要参数的查询,使其成为CSV格式,如由和分隔的字段OPTIONALY。
  3. sftp into the server and grab the file
  4. sftp进入服务器并抓取文件
  5. delete the file from the server
  6. 从服务器删除文件

Method 2: Copy/paste

I tend to do this method. For me it seems a little faster but that's mostly because I don't remember how to construct the SELECT INTO OUTFILE query from above and have to look it up.

我倾向于用这种方法。对我来说,它似乎有点快,但这主要是因为我不记得如何从上面构造SELECT INTO OUTFILE查询,并且必须查找它。

  1. Copy/paste to a local text file
  2. 复制/粘贴到本地文本文件
  3. Open in a text editor and replace | with ,
  4. 在文本编辑器中打开并将|替换为,
  5. Save as a CSV and open in Numbers.
  6. 保存为CSV并打开数字。

6 个解决方案

#1


9  

How about this?:

这个怎么样?:

mysql -B -e "$MY_QUERY" > my_data.csv

mysql -B -e“$MY_QUERY”> my_data.csv

The output format is actually tab-separated rather than comma-separated but at least Excel and OpenOffice Calc automatically adapt to this.

输出格式实际上是表分隔的,而不是逗号分隔的,但至少Excel和OpenOffice Calc会自动适应这种格式。

BTW, for convenience and to enable non-interactive execution of mysql commands, I strongly recommend setting up a secure ~/.my.cnf file
(readable only by you) with entries like this:

顺便说一句,为了方便和支持非交互式地执行mysql命令,我强烈建议建立一个安全的~/.my.cnf文件(仅供您阅读),包含如下条目:

[client]
user=YOUR_MYSQL_USER_NAME
password=YOUR_MYSQL_PASSWORD
host=YOUR_MYSQL_SERVER
port=YOUR_MYSQL_SERVER_PORT
WHATEVER_OTHER_OPTIONS_YOU_LIKE

References:

引用:

http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.html

http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.html

--batch, -B

批,- b

Print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file.

使用选项卡作为列分隔符打印结果,每一行都在新行上。使用此选项,mysql不使用历史文件。

Batch mode results in nontabular output format and escaping of special characters. Escaping may be disabled by using raw mode; see the description for the --raw option.

批处理模式导致非表格输出格式和特殊字符的转义。可以使用原始模式禁用转义;参见“原始选项”的描述。

#2


7  

You can also use the mysql command line utility with the -e option and pipe the output to a file. This will output data in tab-delimited format.

您还可以使用mysql命令行工具和-e选项,并将输出传输到一个文件。这将以表分隔的格式输出数据。

#3


2  

You can execute a SELECT ... INTO OUTFILE query on your local computer, and have it save the file locally. If you've got the mysql* command-line utility installed locally, simply add the -h flag to connect to a specific host. mysql can also read from standard input, so you can set up a .sql file that you'll use to import.

您可以执行SELECT…进入本地计算机上的外文件查询,并让它在本地保存文件。如果在本地安装了mysql*命令行实用程序,只需添加-h 标志,以连接到特定的主机。mysql还可以从标准输入读取数据,因此您可以设置一个用于导入的.sql文件。

SQL file (outfile.sql):

SQL文件(outfile.sql):

SELECT * FROM myTable WHERE date>CURDATE()
INTO OUTFILE '/home/me/outfile.csv' -- Specify output file (if Windows, make sure
                                    -- to use backslashes and escape them 
                                    -- (C:\\Users\\\\...)
FIELDS
    OPTIONALLY ENCLOSED BY '"'
    ESCAPED BY '"' -- Results in double quotation marks, which I have found Excel
                   -- requires (instead of using a backslash to escape)
    TERMINATED BY '\\n' -- or your line ending of choice
                        -- (i believe escaping is required)

Then you would issue the following command:

然后您将发出以下命令:

mysql -u  -p -h  

Note that it might be a good idea to create a new user that can connect from your remote IP address, with FILE permissions and only SELECT access to the tables you need to query.

注意,创建一个新用户可能是一个好主意,该用户可以使用文件权限从远程IP地址连接,并且只选择访问需要查询的表。

It's a little bit of setup initially, but in the end, you can just stick the mysql command in a batch file and make it a two-click process.

一开始只是做了一些设置,但是最后,您可以将mysql命令插入到一个批处理文件中,并使其成为一个双击的过程。

*I believe you have to download the full MySQL system, which comes with the mysql CLI.

*我认为你必须下载MySQL CLI的完整MySQL系统。

#4


2  

Use your first method with the following modification:

使用您的第一个方法和以下修改:

Instead of running the command on the server you can run it on your local machine and save the file directly, without the need to sftp and delete it. For that you have to ssh forward the mysql port to a free port on your local machine.

与在服务器上运行命令不同,您可以在本地机器上运行它并直接保存文件,而不需要sftp和删除它。为此,必须将mysql端口转发到本地机器上的一个空闲端口。

ssh -L 3306:localhost:3306 user@remoteserver

The first "3306" is the port on the local machine. "localhost" refers to the remoteservers hostname. The second "3306" is the port on the remote machine, which shall be forwarded. If the MySQL Server runs on a different port you have to use that port instead. After logging in, you keep the ssh session open as long as you want to work.

第一个“3306”是本地机器上的端口。“localhost”指的是remoteservers主机名。第二个“3306”是远程机器上的端口,需要转发。如果MySQL服务器在另一个端口上运行,则必须使用该端口。登录后,只要您想要工作,就保持ssh会话打开。

In a new terminal window you can then start your mysql session.

在一个新的终端窗口,你可以开始你的mysql会话。

mysql -hlocalhost -uUser -p --port=3306

This method has the advantage, that you don't have to expose your MySQL Server to the Internet and you are transfering all Data through an encrypted tunnel.

这种方法的优点是,您不必将MySQL服务器公开到Internet上,并且您正在通过加密隧道传输所有数据。

#5


0  

Your option 1 is the best I think - do it once and print the correct options or even better, create a small script that will accept a query and automatically add your favorite enclosing & delimiter options.

我认为您的选项1是最好的——一次执行一次并打印正确的选项,甚至更好,创建一个小脚本,它将接受查询并自动添加您最喜欢的封装和分隔符选项。

You can also skip steps 2 and 3 if you install yourself the mysql client utility for your client platform and connect directly to the mysql server from your workstation if possible for you.

如果您为自己安装了客户端平台的mysql客户端实用程序,还可以跳过步骤2和步骤3,如果可能的话,还可以从您的工作站直接连接到mysql服务器。

#6


0  

Excel has a Text to Columns feature under the Data menu which allows you to specify a custom delimeter. I am guessing Numbers would have similar functionality. If it does, then the solution is to copy and paste directly into your Numbers document and then convert those text cells to columns specifying | as the delimiter.

Excel在Data菜单下有一个文本到列的特性,允许您指定一个定制的delimeter。我猜数字会有类似的功能。如果是,那么解决方案是直接复制并粘贴到数字文档中,然后将这些文本单元格转换为指定|作为分隔符的列。


推荐阅读
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • 本文介绍了如何使用PHP代码将表格导出为UTF8格式的Excel文件。首先,需要连接到数据库并获取表格的列名。然后,设置文件名和文件指针,并将内容写入文件。最后,设置响应头部,将文件作为附件下载。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • ubuntu用sqoop将数据从hive导入mysql时,命令: ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有