Home > Database > Mysql Tutorial > How Can I Efficiently Export MySQL Query Results as a CSV File from the Command Line?

How Can I Efficiently Export MySQL Query Results as a CSV File from the Command Line?

Susan Sarandon
Release: 2024-12-17 01:26:25
Original
502 people have browsed it

How Can I Efficiently Export MySQL Query Results as a CSV File from the Command Line?

Exporting MySQL Query Results as CSV with Command-Line Precision

Seeking an efficient way to retrieve MySQL query results in CSV format from the command line? Look no further than this robust solution.

To commence, opt for the MySQL "INTO OUTFILE" command, as outlined in the excerpt from "Save MySQL query results into a text or CSV file":

SELECT order_id,product_name,qty
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Copy after login

Syntax Variations:

Depending on the MySQL version, the syntax may need slight adjustments:

SELECT order_id,product_name,qty
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM orders
WHERE foo = 'bar';
Copy after login

Noteworthy Points:

  1. Column names are not automatically included in the exported CSV.
  2. The target CSV file will be located on the server hosting MySQL.
  3. Adequate write permissions must be granted to the user running the MySQL process.

Remote Server Considerations:

If exporting results from a remote server to your local machine, this solution may not be feasible, especially for hosted or virtualized environments like Heroku and Amazon RDS.

The above is the detailed content of How Can I Efficiently Export MySQL Query Results as a CSV File from the Command Line?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template