Export/Import CSV files with MySQL

來源:互聯網
上載者:User
文章目錄
  • Exporting data as CSV file directly using simple MySQL Query
  • Importing data from CSV file directly using simple MySQL Query
  • Similar Posts:
Export/Import CSV files with MySQL – No external tool required

Loading data from and to CSV or other TAB DELIMITED or
similar file format is essential in day to day operation. With MySQL it
is easy to load data into table using files and exporting data into CSV
files is quite easy. No need to use any external tool it can be done
right there from the query tool/command prompt, no specific GUI tool
required.

Let's see how?

Exporting data as CSV file directly using simple MySQL Query

Simply add INTO OUTFILE [FILE_PATH] in your query and you are done. Here is a simple example:

SELECT * FROM [TABLE]
INTO OUTFILE 'X:/[FILE]'

Above statement uses default options for file export of result set of
a query, but it is simple to override them. By default file will be TAB
DELIMITED file. We can always override as showed in example below:

SELECT * FROM  [TABLE] INTO OUTFILE 'X:/[FILE]'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY 'n'

OR

SELECT * INTO OUTFILE 'X:/[FILE]'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY 'n'
FROM  [TABLE]

The beauty is it don't really matter where you place FROM TABLE
clause, any of the style would work from above example. Don't forget
that we can use all our complex join or where conditions which we really
use with SELECT. 

Full Syntex:

SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
Importing data from CSV file directly using simple MySQL Query

Here is the simplest way to do it:

LOAD DATA INFILE 'X:/[FILE]'
INTO TABLE [TABLE]

Similar to Export option of MySQL it will expect file to be TAB
DELIMITED if we don't specify any option for it in it's simplest form.
So if you simple EXPORT file without any custom specification as a first
query example it would simple LOAD DATA from that file, but if you have
a file with custom separator than it needs to be specified.

LOAD DATA INFILE 'X:/[FILE]'
INTO TABLE [TABLE]
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY 'n'

Full syntex:

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]

So exporting and importing data from MySQL seems quite easy and we do
not need to write any programming statement to format them just a
simple query can do it.

References: (For more reference please refer)
http://dev.mysql.com/doc/refman/5.0/en/select.html
http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Similar Posts:
  • HeidiSQL 5.1 – good has got better
  • HeidiSQL 4.0 RC1 released
  • Using Views to avoid cross database connection – MySQL
  • Alternate to FIND_IN_SET for non-MySQL databases
  • MySQL 5.5.8 – Problem while Creating new User (SQL Error (1364): Field ‘authentication_string’ doesn’t have a default value)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.