MySQL 转储到 CSV 文本文件中,列名位于顶部?
我使用此 SQL 片段将表转储到 CSV 文本文件中:
SELECT * FROM Brand INTO OUTFILE "e: /品牌.csv” 以 ',' 结尾的字段,以 '"' 括起 以“\n”结尾的行;
但是,此方法不会在 CSV 文件的开头添加列名称。我的问题是如何选择所有列/字段名称,就像导出表并选择“将字段名称放在第一行”时 phpMyAdmin 所做的那样。
Possible Duplicate:
How to export / dump a MySql table into a text file including the field names (aka headers or column names)
I use this SQL snippet to dump a table into CSV text files:
SELECT * FROM brand INTO OUTFILE "e:/brand.csv"
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY "\n";
However this approach doesn't add the column names at the beginning of the CSV file. My question is how to select all the column / field names as well, just like what phpMyAdmin does when you export the table and select "Put fields names in the first row".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一种方法,只要您运行 MySQL 5 或更高版本,就必须手动输入这些名称。就是这样,编写为在 unix 命令行上运行的 bash 脚本:
虽然不是最优雅的解决方案,但我确信比我更了解 SQL 和/或 bash 的人可以将其压缩为一行...
它的作用是:
祝你好运,如果你清理了它,请发布你的结果!
I figured out a way around having to manually enter those names as long as you're running MySQL 5 or higher. Here it is, written as a bash script for running on a unix command line:
While not the most graceful solution, i'm sure it can be compressed into a single line by someone who knows SQL and/or bash a little better than me...
What it does is:
Good luck, and if you clean it up, post your results!
我想这就是你想要的。我使用的是 mysql 5.1.60,它在第一行输出字段名称。这将使用“\t”作为字段分隔符,我不知道如何要求逗号代替。
I think this does what you want. I'm using mysql 5.1.60, this outputs field names on the first line. This will use "\t" as the field separator, I'm not sure how to ask for a comma instead.