devroom.io/drafts/2008-11-27-export-csv-directly-from-mysql.md
Ariejan de Vroom dbae98c4c0 Moar updates
2013-03-24 14:27:51 +01:00

26 lines
1001 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Export CSV directly from MySQL"
kind: article
slug: export-csv-directly-from-mysql
created_at: 2008-11-27
tags:
- General
- MySQL
- csv
- quick
- trick
---
How ofter were you asked by a client for a CSV (or excel) file with data from their app? I get asked that question quite often, so I wanted make the process as easy as possible. And guess what? You can create CSV files directly from MySQL with just one query!
Let's say you want to export the id, name and email fields from your users table to a CSV file. Here is your code:
<pre lang="sql">SELECT id, name, email INTO OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY \\
LINES TERMINATED BY '\n'
FROM users WHERE 1</pre>
Well, if you know MySQL, you'll know how to customize this query to spit out the the right data. Your csv file can be found in /tmp/result.csv
Make sure your MySQL server has write permissions to the location where you want to store your results file.