Monday, July 4, 2022

MySQL command to export CSV file from SQL query

Here is the command to export a particular MySQL table to CSV file:

Here is the syntax:

mysql -u root -p 123password123 -D mydatabase -e "SELECT * FROM product WHERE edu_price > 0 INTO OUTFILE '/tmp/edu.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY 'n';"

Break down details of the individual options:

-u (user)

-p (password)

-D (database)

 -e (execute command)

after the query add the following SQL Command:

INTO OUTFILE '/path/filename.csv'

FIELDS TERMINATED BY ','

ENCLOSED BY '"'

LINES TERMINATED BY 'n';

---------------

Please make sure to add ';' (semicolon) at the end of the MySQL command to end the SQL command.


No comments:

Post a Comment