使用 SELECT INTO OUTFILE 时包含标题吗?
使用 MySQL INTO OUTFILE 时是否可以以某种方式包含标头?
Is it possible to include the headers somehow when using the MySQL INTO OUTFILE
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
您必须自己对这些标头进行硬编码。像这样的东西:
You'd have to hard code those headers yourself. Something like:
Joe Steanelli 提供的解决方案可行,但当涉及数十或数百列时,制作列列表很不方便。以下是如何获取my_schema中表my_table的列列表。
现在您可以复制&将结果行粘贴为 Joe 方法中的第一个语句。
The solution provided by Joe Steanelli works, but making a list of columns is inconvenient when dozens or hundreds of columns are involved. Here's how to get column list of table my_table in my_schema.
Now you can copy & paste the resulting row as first statement in Joe's method.
对于使用 ORDER BY 进行复杂选择,我使用以下命令:
For complex select with ORDER BY I use the following:
这将允许您订购列和/或限制
This will alow you to have ordered columns and/or a limit
您可以将准备好的语句与 lucek 的答案一起使用,并动态导出包含 CSV 中的列名称的表:
感谢 lucek。
You can use prepared statement with lucek's answer and export dynamically table with columns name in CSV :
Thank lucek.
我只需进行 2 个查询,第一个是获取带有列名称的查询输出(限制 1)(无硬编码,连接、排序依据、自定义列名称等没有问题),第二个是进行查询本身,并将文件合并到一个 CSV 中文件:
I simply make 2 queries, first to get query output (limit 1) with column names (no hardcode, no problems with Joins, Order by, custom column names, etc), and second to make query itself, and combine files into one CSV file:
我在 NodeJS 中的大表上执行 mysql 查询时遇到了类似的问题。我在 CSV 文件中包含标题的方法如下
使用 OUTFILE 查询准备不带标题的文件
获取第 1 点中使用的表的列标题
使用 prepend-file npm package
每个步骤的执行都是使用 NodeJS 中的 Promise 来控制的。
I faced similar problem while executing mysql query on large tables in NodeJS. The approach which I followed to include headers in my CSV file is as follows
Use OUTFILE query to prepare file without headers
Fetch column headers for the table used in point 1
Append the column headers to the file created in step 1 using prepend-file npm package
Execution of each step was controlled using promises in NodeJS.
如果您熟悉 Python 或 R,并且您的表可以放入内存,那么这是一个替代作弊方法。
将 SQL 表导入 Python 或 R,然后从那里导出为 CSV,您将获得列名称和数据。
这是我使用 R 实现的方法,需要 RMySQL 库:
这有点作弊,但我发现当我的列数太长而无法使用上面的 concat 方法时,这是一个快速的解决方法。注意:R 会在 CSV 的开头添加一个“row.names”列,因此如果您确实需要依赖 CSV 来重新创建表,则需要删除该列。
This is an alternative cheat if you are familiar with Python or R, and your table can fit into memory.
Import the SQL table into Python or R and then export from there as a CSV and you'll get the column names as well as the data.
Here's how I do it using R, requires the RMySQL library:
It's a bit of a cheat but I found this was a quick workaround when my number of columns was too long to use the concat method above. Note: R will add a 'row.names' column at the start of the CSV so you'll want to drop that if you do need to rely on the CSV to recreate the table.
我认为如果您使用 UNION 它将起作用:
我不知道直接使用 INTO OUTFILE 语法指定标头的方法。
I think if you use a UNION it will work:
I don't know of a way to specify the headers with the INTO OUTFILE syntax directly.
由于“include-headers”功能似乎还没有内置,并且这里的大多数“解决方案”需要手动输入列名称,和/或什至不考虑连接,我建议解决问题。
到目前为止,我发现的最好的替代方案是使用一个不错的工具(我使用HeidiSQL)。
提出您的请求,选择网格,只需右键单击并导出到文件。它提供了干净导出所需的所有必要选项,并且应该可以满足大多数需求。
同样的想法,user3037511 的方法运行良好,并且可以轻松实现自动化。
只需使用一些命令行启动您的请求即可获取标头。您可以使用 SELECT INTO OUTFILE... 或通过运行不受限制的查询来获取数据,由您选择。
请注意,输出重定向到文件在 Linux 和 Windows 上都像一个魅力。
这让我想强调,80% 的情况下,当我想使用 SELECT FROM INFILE 或 SELECT INTO OUTFILE 时,由于某些限制(此处缺少AWS-RDS 上的“标头选项”、缺少的权限等等。)
因此,我没有完全回答操作员的问题...但它应该回答他的 >需求 :)
编辑:并实际回答他的问题:不
截至 2017 年 9 月 7 日,如果您坚持使用 SELECT INTO OUTFILE 命令,则无法包含标头:|
Since the 'include-headers' functionality doesn't seem to be build-in yet, and most "solutions" here need to type the columns names manually, and/or don't even take joins into account, I'd recommand to get around the problem.
The best alternative I found so far is using a decent tool (I use HeidiSQL).
Put your request, select the grid, just right click and export to a file. It got all necessary options for a clean export, ans should handle most needs.
In the same idea, user3037511's approach works fine, and can be automated easily.
Just launch your request with some command line to get your headers. You may get the data with a SELECT INTO OUTFILE... or by running your query without the limit, yours to choose.
Note that output redirect to a file works like a charm on both Linux AND Windows.
This makes me want to highlight that 80% of the time, when I want to use SELECT FROM INFILE or SELECT INTO OUTFILE, I end-up using something else due to some limitations (here, the absence of a 'headers options', on an AWS-RDS, the missing rights, and so on.)
Hence, I don't exactly answer to the op's question... but it should answer his needs :)
EDIT : and to actually answer his question : no
As of 2017-09-07, you just can't include headers if you stick with the SELECT INTO OUTFILE command :|
最简单的方法是自己对列进行硬编码,以更好地控制输出文件:
The easiest way is to hard code the columns yourself to better control the output file:
实际上,即使使用 ORDER BY 也可以使其工作。
只需在 order by 语句中需要一些技巧 - 我们使用 case 语句并将标头值替换为保证在列表中排在第一位的其他值(显然这取决于字段的类型以及您是否要对 ASC 或 ASC 进行排序) DESC)
假设您有三个字段:name (varchar)、is_active (bool)、date_something_happens (date),并且您想要对后两个字段进行降序排序:
Actually you can make it work even with an ORDER BY.
Just needs some trickery in the order by statement - we use a case statement and replace the header value with some other value that is guaranteed to sort first in the list (obviously this is dependant on the type of field and whether you are sorting ASC or DESC)
Let's say you have three fields, name (varchar), is_active (bool), date_something_happens (date), and you want to sort the second two descending:
这是一种从列名动态获取标题标题的方法。
Here is a way to get the header titles from the column names dynamically.
我的数据库中的一个例子
表名称传感器和列(id、时间、单位)
an example from my database
table name sensor with colums (id,time,unit)
如果您使用 MySQL Workbench:
从“SCHEMAS”选项卡中选择所有列 ->右键单击->复制到
剪贴板->姓名
将其粘贴到任何文本编辑器中,然后将“`”替换为“'”
将其复制回来并在您的 UNION 查询中使用它(如接受的
答案):
If you are using MySQL Workbench:
Select all the columns from the SCHEMAS tab -> Right Click -> Copy to
Clipboard -> Name
Paste it in any text editor and, Replace " ` " with " ' "
Copy it back and use it in your UNION query (as mentioned in the accepted
answer):
受到 Rick James 的数据透视表示例的启发。
它从
INFORMATION_SCHEMA.COLUMNS
表中获取列列表,并使用GROUP_CONCAT
准备包含带有列名称的字符串列表的SELECT
语句。接下来添加
UNION
和SELECT * FROM指定的database.table
- 这将创建查询文本,该文本将在结果中输出列名和列值。现在,使用之前创建的查询(存储在 @sql 变量中)准备语句,CSV 输出特定的“事物”将附加到查询中,最后使用
execute stmt
执行语句Inspired by pivot table example from Rick James.
It gets list of columns from
INFORMATION_SCHEMA.COLUMNS
table, and usesGROUP_CONCAT
to prepareSELECT
statement with list of strings with column names.Next
UNION
is added withSELECT * FROM specified database.table
- this creates query text that will output both column names and column values in result.Now the statement is prepared using previously created query (stored in
@sql
variable), CSV output specific "things" are appended to query and finally statement is executed withexecute stmt
我正在用 PHP 编写代码,在使用 concat 和 union 函数时遇到了一些麻烦,而且也没有使用 SQL 变量,无论我如何让它工作,这里是我的代码:
I was writing my code in PHP, and I had a bit of trouble using concat and union functions, and also did not use SQL variables, any ways I got it to work, here is my code:
因此,如果
my_table
中的所有列都是字符数据类型,我们可以将最热门的答案(由 Joe、matt 和 illguc 提供)组合在一起,以自动添加标题在一个“简单”的 SQL 查询中,例如最后几行输出 csv。
请注意,如果
my_table
非常大,这可能会很慢。So, if all the columns in
my_table
are a character data type, we can combine the top answers (by Joe, matt and evilguc) together, to get the header added automatically in one 'simple' SQL query, e.g.where the last couple of lines make the output csv.
Note that this may be slow if
my_table
is very large.我想补充 Sangam Belose 提供的答案。这是他的代码:
但是,如果您没有在变量中设置
“secure_file_priv”
,则它可能无法工作。为此,请通过以下方式检查该变量上设置的文件夹:输出应如下所示:
您可以更改此变量或更改查询以将文件输出到显示的默认路径。
I would like to add to the answer provided by Sangam Belose. Here's his code:
However, if you have not set up your
"secure_file_priv"
within the variables, it may not work. For that, check the folder set on that variable by:The output should look like this:
You can either change this variable or change the query to output the file to the default path showing.
仅 MySQL 不足以简单地完成此任务。下面是一个 PHP 脚本,它将把列和数据输出到 CSV。
在顶部附近输入您的数据库名称和表。
您需要将其作为您想要输出到的目录。 MySQL需要具有写入目录的能力。
您可以在查询中编辑 CSV 导出选项:
最后有一个对 GZip CSV 的 exec 调用。
MySQL alone isn't enough to do this simply. Below is a PHP script that will output columns and data to CSV.
Enter your database name and tables near the top.
You'll need this to be the directory you'd like to output to. MySQL needs to have the ability to write to the directory.
You can edit the CSV export options in the query:
At the end there is an exec call to GZip the CSV.
我对这些都没有运气,所以在找到解决方案后,我想将其添加到之前的答案中。 Python==3.8.6 MySQL==8.0.19
(请原谅我没有对 foo 进行如此格式化。请有人清理一下。)
请注意以下几点:
首先,返回列名的查询不能使用标点符号。在“schema_name”和“table_name”周围使用“反引号”或省略“引号”将引发“未知列”错误。
其次,列标题名称作为单实体元组返回,所有列名称连接在一个带引号的字符串中。转换为引用列表很容易,但并不直观(至少对我来说)。
第三,游标必须被缓冲,否则“懒惰”的东西将无法在您需要时获取您的结果。对于非常大的表,内存可能是一个问题。也许分块可以解决这个问题。
最后,所有类型的 UNION 尝试都给我带来了错误。通过将整个混乱压缩到字典列表中,使用 csv.DictWriter 写入 csv 变得微不足道。
I had no luck with any of these, so after finding a solution, I wanted to add it to the prior answers. Python==3.8.6 MySQL==8.0.19
(Forgive my lack of SO formatting foo. Somebody please clean up.)
Note a couple of things:
First, the query to return column names is unforgiving of punctuation. Using ` backticks or leaving out ' quote around the 'schema_name' and 'table_name' will throw an "unknown column" error.
Second, the column header names return as a single-entity tuple with all the column names concatenated in one quoted string. Convert to quoted list was easy, but not intuitive (for me at least).
Third, cursor must be buffered or the "lazy" thing will not fetch your results as you need them. For very large tables, memory could be an issue. Perhaps chunking would solve that problem.
Last, all types of UNION attempts yielded errors for me. By zipping the whole mess into a list of dicts, it became trivial to write to a csv, using csv.DictWriter.
使用 python 的解决方案,但如果您已经使用其他工具,则无需安装 python 包来读取 sql 文件。
如果您不熟悉 python,您可以在 colab 笔记本中运行 python 代码,所有必需的软件包都已安装。它使马特和乔的解决方案自动化。
首先执行此 SQL 脚本以获取包含所有表名称的 csv:
然后将tables.csv 移动到合适的目录,并在替换“path_to_tables”和“your_schema”后执行此 python 代码。它将生成一个 sql 脚本来导出所有表标题:
然后执行此 python 代码,该代码将生成一个 sql 脚本来导出所有表的值:
执行两个生成的 sql 脚本并将标题 csv 和值 csv 移动到您选择的目录中。
然后执行最后一个 python 代码:
然后您将所有表导出为带有标题的 csv,而无需编写或复制粘贴所有表和列名称。
Solution using python but no need to install a python package to read sql files if you already use another tool.
If you are not familiar with python you can run the python codes in a colab notebook, all the required packages are already installed. It automates Matt and Joe's solutions.
Firstly execute this SQL script to get a csv with all table names :
Then move tables.csv to a suitable directory and execute this python code after having replaced 'path_to_tables' and 'your_schema'. It will generate a sql script to export all tables headers:
Then execute this python code which will generate a sql script to export the values of all tables:
Execute the two generated sql scripts and move the header csvs and values csvs in directories of your choice.
Then execute this last python code :
Then you got all your table exported in csv with the headers without having to write or copy paste all the tables and columns names.
之后您始终可以使用 Bash 添加列:
对于大型 CSV 文件,这可能需要一段时间,并且需要两倍于文件大小的可用空间,以便 sed 创建其自动备份文件,但它完成工作。
You can always add the columns afterwards using Bash:
This might take a while for large CSV files and will require free space double the size of the file in order for
sed
to create its automatic backup file, but it does the job.您可以按照文档使用格式文本标题 -
https://docs.aws.amazon.com/ AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Integrating.SaveIntoS3.html
我使用类似的东西:
SELECT * FROM table
INTO OUTFILE S3 's3://bucket/folder'
设置文本标题格式
以“,”结尾的字段
可选择用“”括起来
以 '\n' 结尾的行
覆盖
尝试并让我知道
you could use FORMAT TEXT HEADER as per document -
https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Integrating.SaveIntoS3.html
I use something like:
SELECT * FROM table
INTO OUTFILE S3 's3://bucket/folder'
FORMAT TEXT HEADER
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
OVERWRITE ON
try and let me know