如何将大表导出为 50 个较小的 csv 文件,每个文件包含 100,000 条记录
我正在尝试将一个非常大的表(例如包含 5,000,000 条记录)中的一个字段导出到 csv 列表中,但不是全部导出,而是将 100,000 条记录导出到创建的每个 .csv 文件中,而不重复。请问我该怎么做?
我尝试
SELECT field_name
FROM table_name
WHERE certain_conditions_are_met
INTO OUTFILE /tmp/name_of_export_file_for_first_100000_records.csv
LINES TERMINATED BY '\n'
LIMIT 0 , 100000
给出前 100000 条记录,但我没有采取任何措施将其他 4,900,000 条记录导出到其他 49 个文件中 - 我如何指定其他 49 个文件名?
例如,我尝试了以下操作,但 SQL 语法是错误的:
SELECT field_name
FROM table_name
WHERE certain_conditions_are_met
INTO OUTFILE /home/user/Eddie/name_of_export_file_for_first_100000_records.csv
LINES TERMINATED BY '\n'
LIMIT 0 , 100000
INTO OUTFILE /home/user/Eddie/name_of_export_file_for_second_100000_records.csv
LINES TERMINATED BY '\n'
LIMIT 100001 , 200000
并且没有创建第二个文件...
请问我做错了什么,有更好的方法吗?是否应该将 LIMIT 0 , 100000 放在第一个 INTO OUTFILE 语句之前,然后对第二个 100,000 条记录重复 SELECT 中的整个命令,等等?
感谢您的任何帮助。
艾迪
I am trying to export one field from a very large table - containing 5,000,000 records, for example - into a csv list - but not all together, rather, 100,000 records into each .csv file created - without duplication. How can I do this, please?
I tried
SELECT field_name
FROM table_name
WHERE certain_conditions_are_met
INTO OUTFILE /tmp/name_of_export_file_for_first_100000_records.csv
LINES TERMINATED BY '\n'
LIMIT 0 , 100000
that gives the first 100000 records, but nothing I do has the other 4,900,000 records exported into 49 other files - and how do I specify the other 49 filenames?
for example, I tried the following, but the SQL syntax is wrong:
SELECT field_name
FROM table_name
WHERE certain_conditions_are_met
INTO OUTFILE /home/user/Eddie/name_of_export_file_for_first_100000_records.csv
LINES TERMINATED BY '\n'
LIMIT 0 , 100000
INTO OUTFILE /home/user/Eddie/name_of_export_file_for_second_100000_records.csv
LINES TERMINATED BY '\n'
LIMIT 100001 , 200000
and that did not create the second file...
what am I doing wrong, please, and is there a better way to do this? Should the LIMIT 0 , 100000 be put Before the first INTO OUTFILE statement, and then repeat the entire command from SELECT for the second 100,000 records, etc?
Thanks for any help.
Eddie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在类 UNIX 操作系统上运行,为什么不直接选择全部并通过管道传输输出:
作为概念证明:
创建三个文件
xaa
、xab
和包含行1,2,3
、4,5,6
和7,8,9
的xac
分别。或者,即使在其他操作系统上,您也可以获得 GNU 工具,例如 GnuWin32,其中
split
位于 coreutils 中。If you're running on a UNIX-like OS, why not just select the whole lot and pipe the output through:
As proof of concept:
creates three files
xaa
,xab
andxac
containing the lines1,2,3
,4,5,6
and7,8,9
respectively.Or, even on other operating systems, you can get the GNU tools, like GnuWin32, where
split
is in coreutils.您可以使用循环和子查询来生成文件。以下过程可以告诉您如何做到这一点(可能有语法错误):
You can use loop and sub-query to generate the files. following procedure can give you clue how to do that(it may have syntax error):