MySQL Database export CSV file command:
Mysql> Select First_name,last_name,email from account to outfile ' e://output1.csv ' fields terminated by ', ' Optionall Y enclosed by "lines terminated by '/n ';
CSV file effect:
Sunny |
Grigoryan |
[Email protected] |
|
|
|
|
|
Jon |
Siegal |
[Email protected] |
|
|
|
|
|
Joe |
Siegal |
[Email protected] |
|
|
|
|
|
Alejandro |
Medina |
[Email protected] |
|
|
|
|
|
CVS file import mysql database command:
mysql> Load Data local infile ' e://input1.csv ' into table test1 fields Termin
Ated by ', ' lines terminated by '/n ' (first_name,last_name,email);
Query OK, 1 row affected, 1 Warning (0.00 sec)
records:69 deleted:0 skipped:68 warnings:0
Mysql> select * from Test1;
+----+------------+-----------+--------------------------+
| ID | first_name | last_name | email |
+----+------------+-----------+--------------------------+
| 0 | Sunny | Grigoryan | [Email protected]
+----+------------+-----------+--------------------------+
Fields TERMINATED by----field terminating character
Optionally enclosed by----envelope character
LINES TERMINATED by----line Terminator
Connect to the server through the MySQL client shell, select the database to use, and enter the SQL code:
SELECT * FROM Test_info
into outfile '/tmp/test.csv '
Fields terminated by ', ' optionally-enclosed by ' "' escaped by '" '
Lines terminated by ' \ r \ n ';
The most critical part of this is the format parameter.
This parameter is set according to the RFC4180 document, which is the full name common format and MIME Type for comma-separated Values (CSV) Files, which detail the CSV format, and the key points include:
(1) The fields are separated by commas, and the data rows are separated by \ r \ n;
(2) The string is surrounded by a half-width double quotation mark, and the string itself is represented by a double quotation mark in two double quotes.
By executing the code above, you can export the required data in CSV format to the executed file.
In addition, the SQL code for importing CSV format data in MySQL is as follows:
Load data infile '/tmp/test.csv '
into table Test_info
Fields terminated by ', ' optionally-enclosed by ' "' escaped by '" '
Lines terminated by ' \ r \ n ';
The following statements are useful when the number of columns in a CSV file is less than the database table:
Load data infile "/tmp/appleappv2_search_day_20110525.csv" into table Apple_search_log fields terminated by '; ' Lines term Inated by '/n ' (Apptypeid,keyword,searchtime,model);
The blue Word part is the highlight. This statement is useful if the data table contains self-increment fields, such as the self-increment ID.
MySQL Export import data-csv