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.

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

更多相关文章

  1. MySQL的ibdata1文件占用过大瘦身
  2. 如何使用Angular.js从json文件中使用id获取特定数据
  3. 如何从一个节点生成exe文件。js应用?
  4. 在批处理文件中使用JSMin
  5. 使用HTML文件中的React调用.js文件中的Javascript函数
  6. 访问D3的var格式数据
  7. 在JavaScript中进行文件处理,第四部分:对象URLs
  8. [转]在网页中加入声音文件,并且用JavaScript对其进行播放控制
  9. js金额数字格式化实现代码(三位加逗号处理保留两位置小数)

随机推荐

  1. Android Security
  2. Android APK 扩展文件
  3. Android ViewPager使用详解
  4. Android 5.0(L) 的MediaSession架构分析(
  5. android通知栏提示
  6. Android 获取Sim卡联系人
  7. Android改变MAC地址
  8. Stuff about Android
  9. Android 自动更新代码
  10. BBC发布了android客户端应用