At times where we need to export Data into csv format in mariadb/mysql,
SELECT INTO OUTFILE will write resulting rows from a query into a file - csv..
Below is a simple example on this :
select * from table INTO OUTFILE '/path/test.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ' " '
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
Handling Null values -
Another thing we will notice is having /N in output where we have Null values in table, in few cases requirement is to have blank Fields instead of /N in output. in these cases we need to use IFNULL function in the query
example,
Select name, IFNULL (Date, ' ') from table INTO OUTFILE '/path/test.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ' " '
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
With the above we replaced Null with blank in the csv output.
SELECT INTO OUTFILE will write resulting rows from a query into a file - csv..
Below is a simple example on this :
select * from table INTO OUTFILE '/path/test.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ' " '
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
Handling Null values -
Another thing we will notice is having /N in output where we have Null values in table, in few cases requirement is to have blank Fields instead of /N in output. in these cases we need to use IFNULL function in the query
example,
Select name, IFNULL (Date, ' ') from table INTO OUTFILE '/path/test.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ' " '
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
With the above we replaced Null with blank in the csv output.
No comments:
Post a Comment