1285

Is there an easy way to run a MySQL query from the Linux command line and output the results in CSV format?

Here's what I'm doing now:

mysql -u uid -ppwd -D dbname << EOQ | sed -e 's/        /,/g' | tee list.csv
select id, concat("\"",name,"\"") as name
from students
EOQ

It gets messy when there are a lot of columns that need to be surrounded by quotes, or if there are quotes in the results that need to be escaped.

codeforester
  • 28,846
  • 11
  • 78
  • 104
MCS
  • 20,445
  • 20
  • 56
  • 76
  • 1
    You can use [`REPLACE()`](http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace) in your query to have the quotes escaped. – dsm Dec 10 '08 at 16:08
  • Look at my answer in [this stackoverflow][1] [1]: http://stackoverflow.com/questions/12242772/easiest-way-to-copy-a-table-from-one-database-to-another/29395487#29395487 – biniam Apr 01 '15 at 16:14
  • You can [Format Table Data as Text table](http://stackoverflow.com/a/34084279/1045444). – Somnath Muluk Dec 04 '15 at 09:45
  • 4
    The accepted answer to this stackoverflow question is probably the best way: http://stackoverflow.com/questions/3760631/mysql-delimiter-question – Samuel Åslund Jan 22 '16 at 16:28
  • I wrote a feature request on the MariaDB bug tracker (https://jira.mariadb.org/browse/MDEV-12879) You can vote on it. – Jared Beck May 23 '17 at 18:27
  • https://stackoverflow.com/questions/1119312/mysql-export-into-outfile-csv-escaping-chars – Sunil Rajput Jun 05 '17 at 11:27
  • Copied from a below deleted answer: `SELECT @@GLOBAL.secure_file_priv;` will show you the path you do have access to. I did not have write access to `/tmp/` but I did to `/var/lib/mysql-files/`. – Chloe Dec 19 '18 at 18:33
  • Possible answer if you do not have write access on the server: 1) Use mysqldump to dump the table. 2) Import the table into your local database. 3) Use accepted answer to export (`select ... into outfile`) into CSV. – Chloe Dec 19 '18 at 19:04
  • I am guessing you mean without programming code, right? – LongChalk Feb 13 '20 at 14:06

39 Answers39

1883

From http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/

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

Note: That syntax may need to be re-ordered to

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';

in more recent versions of MySQL.

Using this command columns names will not be exported.

Also note that /var/lib/mysql-files/orders.csv will be on the server that is running MySQL. The user that the MySQL process is running under must have permissions to write to the directory chosen, or the command will fail.

If you want to write output to your local machine from a remote server (especially a hosted or virtualize machine such as Heroku or Amazon RDS), this solution is not suitable.

Paul Tomblin
  • 167,274
  • 56
  • 305
  • 392
  • 1
    @TomasT. there are a bunch of solutions below that get you tab delimited instead of CSV. Excel, for instance, can take both CSV and tab delimited, and convert one to the other. – Paul Tomblin Oct 22 '11 at 12:32
  • 16
    @Tomas if you have access to a remote filesystem and MySQL, you must be able to write somewhere. instead of /tmp, try /home/yourusername/file.csv -- if that fails, and the result set is not that large, you could copy the output from your SSH client and paste to your local machine. – Michael Butler Mar 09 '12 at 15:40
  • 1
    Unfortunately this is not standards-compliant in any way – user694971 Jul 11 '12 at 13:00
  • 65
    The question specified MySQL, not "standards compliant". – Paul Tomblin Jul 11 '12 at 13:28
  • 37
    How to include header as well? – Bogdan Gusiev Jan 25 '13 at 10:01
  • 3
    On a Windows machine, the `OUTFILE` string should escape the backslashes. For example, I used the string `'C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\output\\orders.csv'` – styfle Feb 18 '13 at 19:43
  • 66
    @BogdanGusiev, you can include header by prepending "SELECT 'order_id','product_name','qty' UNION" before the real query. First select query returns header, second query returns real data; UNION joins it together. – petrkotek Jun 14 '13 at 04:35
  • @BogdanGusiev have a look at http://lifeboysays.wordpress.com/2012/06/23/mysql-how-to-export-data-to-csv-with-column-headers/ – Paul Tomblin Jun 14 '13 at 13:35
  • 21
    @Michael Butler: Well, no, actually. If you're using Amazon RDS, Heroku, or any other hosted solution, you're unlikely to be able to just write to someone else's server. – Ken Kinder Jun 26 '13 at 15:12
  • what if you want to use it after joins ? SELECT order_id,product_name,qty FROM orders INTO OUTFILE '/tmp/orders.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' join x as x on xx.id = test.id – Daniel Adenew Jul 17 '13 at 13:00
  • 1
    @Tomas: I wrote a tool specifically to handle that. See [this StackOverflow answer](http://stackoverflow.com/questions/15640287/change-output-format-for-mysql-command-line-results-to-csv/17910254#17910254). – spiffytech Jul 28 '13 at 16:19
  • 5
    DB administrators should think carefully before allowing the File_priv permission that's required for this approach to work. See http://docs.oracle.com/cd/E17952_01/refman-5.0-en/security-against-attack.html – mc0e Oct 10 '13 at 08:19
  • 6
    @TMS, You still need the "grant file" privilege for this, see also http://stackoverflow.com/questions/6091427/mysql-into-outfile-access-denied-but-my-user-has-all-access-and-the-fold – Klaas van Schelven Mar 25 '14 at 13:24
  • 5
    If you have a lot of columns and you need them all: `SELECT column_name FROM information_schema.columns WHERE table_name = 'orders' INTO OUTFILE '/tmp/orders_columns.csv' FIELDS TERMINATED BY '' ENCLOSED BY '"' LINES TERMINATED BY ',';` – Dan Sandland Dec 29 '14 at 18:22
  • you can use myadmin export option for csv file – cijagani Aug 05 '15 at 03:15
  • On windows xampp file location is C:\xampp\mysql\data\_\orders.csv ( INTO OUTFILE 'urls1.csv' ) – Mukesh Aug 18 '15 at 11:24
  • 1
    Don't forget the permissions thing. I had to put my outfile in /tmp. mysql has no permissions to write into my working directory. Here is my query > select * from daily_reads where DeviceID=80632679 and LoadDateLocal > '2015-06-01 00:00:00' into outfile '/tmp/80632679_reads_since_060115.csv' fields terminated by ',' enclosed by '"' lines terminated by '\r\n'; – octopusgrabbus Aug 25 '15 at 14:08
  • 7
    This solution only works when you have access to the MYSQL server file system, i.e. you have direct administrative access to that server (and don't mind filling up its drive with data exports). This is not a solution when the user doesn't have access to the server, e.g. the user isn't an admin, or the user is an admin but the database is running remotely e.g. at AWS. – Chris Johnson Jan 31 '16 at 19:39
  • 4
    I don't think this works well if you have line breaks or double quotes in your records... There does not seem to be a method to replace " with "" (which I believe is the custom in CSV) in the output without also escaping line breaks as well. – runamok Aug 17 '16 at 06:09
  • @styfle ,well, on windows you can try `/` as delimiter like `'C:/wamp/bin/mysql/mysql5.5.24/bin/output/orders.csv'`,it work for me. – LancelotHolmes Oct 29 '16 at 06:48
  • Be aware, if you have strict settings, you may get this: ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement – Kemin Zhou Dec 02 '16 at 21:50
  • 13
    If mysql is running with `--secure-file-priv` you likely can't write to `/tmp`. Run `SHOW VARIABLES LIKE "secure_file_priv";` to see which directory you can write to. – Luke Moore Dec 20 '16 at 21:32
  • I have been using [SQLyog] (https://www.webyog.com/product/sqlyog) to export to csv. It is pretty easy that way. –  Feb 27 '17 at 14:55
  • @PaulTomblin how can i export it with the column name ? – Pranav MS Aug 21 '17 at 06:26
  • What use is a csv without column headers? – Kolob Canyon Sep 04 '17 at 21:39
  • Given that the OP specifically raises the issues that arises with quoting, how is this answer correct? What if there is a `"` in one of the values? – mc0e Oct 07 '17 at 15:59
  • @mc0e I'm pretty sure there is an "ESCAPED BY" clause you can use if you have fields that might have quotes. https://stackoverflow.com/a/2859026/3333 – Paul Tomblin Oct 07 '17 at 18:09
  • @petrkotek UNION does not guarantee order; your header may end up as footer – user2426679 Sep 24 '18 at 19:04
  • I had to use this to import into Postgres because MySQL uses '\' to escape characters like `\"` and `\\r`. `\copy table (field1, field2, created_at, updated_at) from 'files/table.csv' with (format csv, header true, escape e'\\');` (I manually inserted a header with Notepad++.) http://stackoverflow.com/questions/7484413/ddg#7495514 – Chloe Dec 19 '18 at 18:50
  • 1
    Every mysql server I touch has a file called `/var/lib/mysql-files/orders.csv` because every time I need to export to CSV I copy-paste from this answer. – brandones Jun 22 '19 at 01:59
  • Giving error : [HY000][1290] The MySQL server is running with the --secure-file-priv option so it cannot execute this statement – Dinesh Appuhami Aug 01 '19 at 13:15
  • Is there a way to only create the file if the query returns something ? and to not create anything if the row count = 0 ? – Ari Nov 15 '19 at 11:12
  • @PaulTomblin Regard same if i run following query i am getting error. My query : https://justpaste.it/2fp6f Any help thanks. – zus Feb 19 '20 at 05:31
  • Ugh! Is there a way to do this without sending it to a file??? – Michael Apr 02 '20 at 19:44
  • @Michael try the syntax about without the `INTO OUTFILE` part. – Paul Tomblin Apr 03 '20 at 11:54
  • 3
    I am getting `ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement ` not really sure why – Trect Jun 28 '20 at 18:08
  • how to make it append in the same file, if we are using iterate/loop. – kestrel Sep 04 '20 at 18:40
  • currently the statement "INTO OUTFILE " has to be after the projection (select [columns], here is an example https://stackoverflow.com/a/63990228/1706618 – user1706618 Apr 29 '21 at 11:15
509
$ mysql your_database --password=foo < my_requests.sql > out.csv

Which is tab separated. Pipe it like that to get a true CSV (thanks @therefromhere):

... .sql | sed 's/\t/,/g' > out.csv
brent.payne
  • 4,109
  • 3
  • 20
  • 17
Stan
  • 7,968
  • 2
  • 26
  • 30
  • 6
    not only that, but it allows for content to be created from a remote database host, unlike the other of writing to the local filesystem. – tmarthal Jul 08 '11 at 02:38
  • 32
    It's tab-separated, not comma-separated. – Flimm Aug 30 '11 at 15:13
  • 14
    @Flimm, assuming you don't have embedded commas/tabs in the fields you can convert it by piping the result into `| sed 's/\t/,/g'` – John Carter Nov 10 '11 at 04:42
  • 1
    I added `-B` for good measure, but tab-separated is as good as comma-separated for me. – hibbelig Feb 16 '12 at 06:08
  • Pretty interesting. Why the output of this command to > out.csv is different than to stdout if no file redirection is specified? – Fran Marzoa Aug 12 '12 at 17:11
  • 118
    the sed 'fix' does not compensate for commas that may appear in any of the selected data and will skew your columns outputted accordingly – Joey T Dec 11 '12 at 01:17
  • 1
    Is there a solution for @JoeyT's point? I have commas in some of the fields which is obviously pushing the output to the next column – Nathan Waters May 01 '13 at 08:14
  • 1
    @NathanWaters Yes, use the -B (batch) switch mentioned in the previous solution instead of munging the results with sed. – Joey T May 01 '13 at 16:44
  • 2
    This is OK for some data, but if you have arbitrary text fields or binary data in there it's wrong. eg your fields may include tabs, carriage returns, backslashes or quotes. – mc0e Oct 10 '13 at 08:34
  • 15
    The negativity is valid.. it might work for you now but it could well bite you in the future when your data includes a tab or a comma etc.. – John Hunt Apr 30 '14 at 12:44
  • 1
    One trick if you need to open a resultant CSV file in Excel and you potentially have separator characters in your data is to rename the output file with a `txt` extension - the resulting text import into Excel for some reason is way more inclined to separate the data correctly. – Leith Aug 18 '16 at 05:14
  • sed 's/^/\"/g;s/\t/\",\"/g;s/$/\"/g' – Dr. Tyrell Oct 20 '16 at 04:08
  • 1
    The OP specifically raised concerns with quoting the data, so this answer is substantially incorrect. – mc0e Oct 07 '17 at 16:06
  • 4
    Mac OSX does not understand `\t`. You can insert a tab literal using `ctrl` + `v` then `tab` – Kirk Mar 09 '18 at 18:18
  • I could not use the accepted answer, because I got `Access denied` error. This solution worked for me. – Niloofar Jun 12 '19 at 12:25
  • 1
    `| tr '\t' ','` worked better for me. Sed didn't seem to work for this case. – Marcus Oct 28 '19 at 16:26
  • I get an empty csv file. completely blank – Dean P May 01 '20 at 21:07
218

mysql --batch, -B

Print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in non-tabular output format and escaping of special characters. Escaping may be disabled by using raw mode; see the description for the --raw option.

This will give you a tab separated file. Since commas (or strings containing comma) are not escaped it is not straightforward to change the delimiter to comma.

serbaut
  • 5,424
  • 2
  • 26
  • 32
  • 14
    this is a preferred solution: 1) tab-separated-value lists are common in UNIX 2) TSV imports are natively supported by most import systems, including Excel, OpenOffice Spreadsheets, etc. 3) no need to escape quote characters for text fields 4) makes command-line exports a breeze – Joey T Dec 11 '12 at 01:18
  • 6
    this is the best solution because unlike first one need not have permissions on servers like RDS – Muayyad Alsadi Feb 13 '13 at 10:31
  • 8
    A neat trick: if you save the tab separated file as .xls instead of .csv, it will open in excel without any need for "text to data" conversion and without any regional settings issues. – serbaut Apr 11 '13 at 21:05
  • 4
    @Joey T - you still need to escape tabs and carriage returns. Also if the content of a field looks like a quoted or escaped field, the imported content may not look like the original. – mc0e Oct 10 '13 at 08:22
  • Thanks, that will help me a lot in the future! The CLI --help only says: _-B, --batch: Don't use history file. Disable interactive behavior. (Enables --silent.)_ So I always overlooked this option when I was looking for a "plain" export format. – dennis Feb 20 '14 at 08:22
  • I use this in combination with the excellent `xsv`[https://github.com/BurntSushi/xsv] utility to convert to csv – James Jun 14 '18 at 05:29
  • 2
    you will have a problem with NULLs.. they will come out as string nulls.. – Jonathan Oct 10 '18 at 10:18
  • You can skip column headers using `-N` or `--skip-column-names`. – Flimm Jun 20 '19 at 09:07
  • I found that `--batch` doesn't escape newlines consistently, sometimes as `\n`, sometimes as a newline followed by `\n` – Flimm Jun 20 '19 at 10:03
161

Here's a fairly gnarly way of doing it. Found it somewhere, can't take any credit

mysql --user=wibble --password wobble -B -e "select * from vehicle_categories;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > vehicle_categories.csv

Works pretty well. Once again though a regex proves write only.


Regex Explanation:

  • s/// means substitute what's between the first // with what's between the second //
  • the "g" at the end is a modifier that means "all instance, not just first"
  • ^ (in this context) means beginning of line
  • $ (in this context) means end of line

So, putting it all together:

s/'/\'/          replace ' with \'
s/\t/\",\"/g     replace all \t (tab) with ","
s/^/\"/          at the beginning of the line place a "
s/$/\"/          at the end of the line place a "
s/\n//g          replace all \n (newline) with nothing
Jeremy Logan
  • 45,614
  • 37
  • 119
  • 143
Tim Harding
  • 2,485
  • 1
  • 15
  • 10
  • 4
    The -e flag was exactly what I was looking for! Thanks! – Gaurav Gupta Jan 03 '12 at 08:46
  • 1
    This regex is quite simple really; like a lot of other answers on this page, its just cleaning up the output from `mysql -B`. If you separated the regex into individual statements on separate lines, it would be quite simple (for someone who knows regex) to understand. – Mei Mar 09 '12 at 00:58
  • It works great! It's important to say that headers are already included (no extra steps are required). – lepe May 16 '13 at 08:00
  • 7
    AT first glance, this looks pretty broken. Tabs and carriage returns in content will be mishandled. "s/'/\'/;" does nothing at all, because the double quotes in the shell command consume the backslash. Many other similar bugs with the backslash being lost. No handling for backslash in the db field. – mc0e Oct 10 '13 at 08:26
  • 2
    It looks like this command works well on Linux but not in Mac – Hemerson Varela Apr 03 '14 at 19:36
  • 8
    This will break if you have a text field that contains tabs, backslashes, `","`, and a number of other things. A regex is _not_ the way to solve this problem – aidan Apr 14 '14 at 03:59
  • 1
    I propose this ... | sed 's/"/\"/g' | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" ... – snowindy Sep 26 '15 at 10:51
  • this is actually much better and faster in some cases than the internal mysql's CSV export. nicely done! – reflog Oct 04 '17 at 10:42
  • It works when I don't have admin permission, thanks. – Woody Sun Jan 13 '20 at 07:48
103

Unix/Cygwin only, pipe it through 'tr':

mysql <database> -e "<query here>" | tr '\t' ',' > data.csv

N.B.: This handles neither embedded commas, nor embedded tabs.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
strickli
  • 1,263
  • 1
  • 10
  • 6
61

This saved me a couple of times. Fast and it works!

--batch Print results using tab as the column separator, with each row on a new line.

--raw disables character escaping (\n, \t, \0, and \)

Example:

mysql -udemo_user -p -h127.0.0.1 --port=3306 \
   --default-character-set=utf8mb4 --database=demo_database \
   --batch --raw < /tmp/demo_sql_query.sql > /tmp/demo_csv_export.tsv

For completeness you could convert to csv (but be careful because tabs could be inside field values - e.g. text fields)

tr '\t' ',' < file.tsv > file.csv

hrvoj3e
  • 1,667
  • 1
  • 15
  • 16
  • 4
    Unless I'm missing something, this creates a TSV file, not a CSV file... – PressingOnAlways Jul 12 '16 at 00:45
  • @PressingOnAlways Yes. MySQL doc quote: "Print results using tab as the column separator, with each row on a new line." But parsing should not be a problem with any delimiter. I used it to make excel files for my client. – hrvoj3e Jul 12 '16 at 06:50
41

The OUTFILE solution given by Paul Tomblin causes a file to be written on the MySQL server itself, so this will work only if you have FILE access, as well as login access or other means for retrieving the file from that box.

If you don't have such access, and tab-delimited output is a reasonable substitute for CSV (e.g., if your end goal is to import to Excel), then Serbaut's solution (using mysql --batch and optionally --raw) is the way to go.

Leland Woodbury
  • 519
  • 4
  • 3
39

MySQL Workbench can export recordsets to CSV, and it seems to handle commas in fields very well. The CSV opens up in OpenOffice fine.

David Oliver
  • 2,209
  • 21
  • 33
  • 3
    Thanks a million David. After spending 3 hours getting the newlines to output properly for HTML content in the data, I used the MySQL Workbench and in 2 minutes I had my CSV file ready. – mr-euro Jul 11 '12 at 17:02
  • I've just found it can save as XML, too, which is great. I'm hoping to migrate from one application to another by using XSLT to transform this XML into a CSV file suitable for importing into the target application. – David Oliver Aug 05 '12 at 21:27
  • mysql workbench is best option for import export feature. – cijagani Aug 05 '15 at 03:20
  • Well,but each time the workbench limit the select records up to 1000 and when it comes to much more records it does not work that well,the same condition for the import it often blocked if I try to import a relatively large csv file into the mysql database by workbench. – LancelotHolmes Oct 29 '16 at 06:42
  • 1
    I just successfully exported over half a million rows using mysql workbench so large files don't seem to be a problem. You just have to make sure you remove the select limit before running your query and you may also have to increase the following values in your my.ini file: max_allowed_packet = 500M, net_read_timeout = 600, net_write_timeout = 600 – Vincent Apr 22 '18 at 15:47
  • 3
    MySQL workbench has an embarrassing memory leak when export data in CSV, if you have a large data set like 1 or 2 million more rows, 12 GB of RAM ( tested on my linux machine ) are not enough and the memory are not cleared unless you kill or stop it. For large data set this is not a solution. – Ostico May 16 '19 at 15:00
32

How about:

mysql your_database -p < my_requests.sql | awk '{print $1","$2}' > out.csv
dennis
  • 582
  • 5
  • 10
Steve
  • 345
  • 3
  • 2
  • 2
    I really like this one. It is much cleaner, and I like the use of awk. However, I would have probably gone with this: `mysql -uUser -pPassword your_database < my_requests.sql | awk 'BEGIN{OFS="=";} {print $1,$2}' > out.csv` – Josh Apr 11 '12 at 00:00
  • 8
    This fails for field values with spaces. – Barun Sharma Apr 10 '15 at 06:17
31

All of the solutions here to date, except the MySQL workbench one, are incorrect and quite possibly unsafe (ie security issues) for at least some possible content in the mysql db.

MYSQL Workbench (and similarly PHPMyAdmin) provide a formally correct solution, but are designed for downloading the output to a user's location. They're not so useful for things like automating data export.

It is not possible to generate reliably correct csv from the output of mysql -B -e 'SELECT ...' because that cannot encode carriage returns and white space in fields. The '-s' flag to mysql does do backslash escaping, and might lead to a correct solution. However, using a scripting language (one with decent internal data structures that is, not bash), and libraries where the encoding issues have already been carefully worked out is far safer.

I thought about writing a script for this, but as soon as I thought about what I'd call it, it occurred to me to search for pre-existing work by the same name. While I haven't gone over it thoroughly, the solution at https://github.com/robmiller/mysql2csv looks promising. Depending on your application, the yaml approach to specifying the SQL commands might or might not appeal though. I'm also not thrilled with the requirement for a more recent version of ruby than comes as standard with my Ubuntu 12.04 laptop or Debian Squeeze servers. Yes I know I could use RVM, but I'd rather not maintain that for such a simple purpose.

Hopefully someone will point out a suitable tool, that's had a bit of testing. Otherwise I'll probably update this when I find or write one.

mc0e
  • 2,343
  • 23
  • 25
  • 1
    You are right, for complex strings in the table you have to use some decent library, not just bash. I think nodejs has a better solutions for this kind of actions. like this [example](https://github.com/crzidea/mysql2csv/blob/master/index.js) – yeya Jul 28 '16 at 15:28
  • 1
    Hi, good answer, I would add a link to the python solution proposed below https://stackoverflow.com/a/35123787/1504300, which works well and is very simple. I tried to edit your post but the edit's been rejected – reallynice Jun 02 '17 at 12:44
  • Rob Miller's mysql2csv script insists on connecting to the database itself, either via network or socket, and cannot be used as a unix-style pipe. Maybe that's required but it really limits the use. – chrisinmtown Sep 09 '20 at 19:21
  • @chrisinmtown what would you want to pipe into it? Output of myslqldump maybe? As I've pointed out, the output of `mysql -B`can't be fixed. – mc0e Oct 04 '20 at 14:03
  • 1
    @mc0e I want to pipe *in* the output of mysqldump, read that as stdin. – chrisinmtown Oct 05 '20 at 13:20
  • That could be useful for exporting whole tables into csv, but it doesn't allow you to get at the output of queries, so you can't use much of the power of the mysql engine that way. – mc0e Oct 07 '20 at 04:00
30

Many of the answers on this page are weak because they don't handle the general case of what can occur in CSV format. e.g. commas and quotes embedded in fields and other conditions that always come up eventually. We need a general solution that works for all valid CSV input data.

Here's a simple and strong solution in Python:

#!/usr/bin/env python

import csv
import sys

tab_in = csv.reader(sys.stdin, dialect=csv.excel_tab)
comma_out = csv.writer(sys.stdout, dialect=csv.excel)

for row in tab_in:
    comma_out.writerow(row)

Name that file tab2csv, put it on your path, give it execute permissions, then use it like this:

mysql OTHER_OPTIONS --batch --execute='select * from whatever;' | tab2csv > outfile.csv

The Python CSV-handling functions cover corner cases for CSV input format(s).

This could be improved to handle very large files via a streaming approach.

Michael
  • 5,910
  • 4
  • 52
  • 74
Chris Johnson
  • 17,500
  • 5
  • 69
  • 74
  • 9
    An even more dependable solution would be to actually connect to the database with Python, then you should have an easier time doing what you need to do to deal with larger datasets (chunking results, streaming, etc). – Josh Rumbut Feb 04 '16 at 15:52
  • @JoshRumbut, really late, but I made https://stackoverflow.com/a/41840534/2958070 to complement your comment – Ben Aug 30 '19 at 16:28
  • Please see https://stackoverflow.com/questions/356578/how-to-output-mysql-query-results-in-csv-format/63831142#63831142 for an extended version of this script with an input dialect that handles embedded comma and double-quote characters! – chrisinmtown Sep 10 '20 at 13:51
25

From your command line, you can do this:

mysql -h *hostname* -P *port number* --database=*database_name* -u *username* -p -e *your SQL query* | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > *output_file_name.csv*

Credits: Exporting table from Amazon RDS into a csv file

Community
  • 1
  • 1
Sri Murthy Upadhyayula
  • 13,020
  • 1
  • 14
  • 20
20
  1. logic :

CREATE TABLE () (SELECT data FROM other_table ) ENGINE=CSV ;

When you create a CSV table, the server creates a table format file in the database directory. The file begins with the table name and has an .frm extension. The storage engine also creates a data file. Its name begins with the table name and has a .CSV extension. The data file is a plain text file. When you store data into the table, the storage engine saves it into the data file in comma-separated values format.

zloctb
  • 8,712
  • 5
  • 60
  • 76
16

This answer uses Python and a popular third party library, PyMySQL. I'm adding it because Python's csv library is powerful enough to correctly handle many different flavors of .csv and no other answers are using Python code to interact with the database.

import contextlib
import csv
import datetime
import os

# https://github.com/PyMySQL/PyMySQL
import pymysql

SQL_QUERY = """
SELECT * FROM my_table WHERE my_attribute = 'my_attribute';
"""

# embedding passwords in code gets nasty when you use version control
# the environment is not much better, but this is an example
# https://stackoverflow.com/questions/12461484
SQL_USER = os.environ['SQL_USER']
SQL_PASS = os.environ['SQL_PASS']

connection = pymysql.connect(host='localhost',
                             user=SQL_USER,
                             password=SQL_PASS,
                             db='dbname')

with contextlib.closing(connection):
    with connection.cursor() as cursor:
        cursor.execute(SQL_QUERY)
        # Hope you have enough memory :)
        results = cursor.fetchall()

output_file = 'my_query-{}.csv'.format(datetime.datetime.today().strftime('%Y-%m-%d'))
with open(output_file, 'w', newline='') as csvfile:
    # http://stackoverflow.com/a/17725590/2958070 about lineterminator
    csv_writer = csv.writer(csvfile, lineterminator='\n')
    csv_writer.writerows(results)
Michael
  • 5,910
  • 4
  • 52
  • 74
Ben
  • 4,824
  • 3
  • 24
  • 34
  • An answer using python was already provided. http://stackoverflow.com/a/35123787/5470883 – Alexander Baltasar Jan 25 '17 at 09:39
  • 5
    @AlexanderBaltasar, right, and it looks useful, but it's not using Python code to interact with the database. See the comment on that on that question. – Ben Jan 26 '17 at 02:29
13

Also, if you're performing the query on the Bash command line, I believe the tr command can be used to substitute the default tabs to arbitrary delimiters.

$ echo "SELECT * FROM Table123" | mysql Database456 | tr "\t" ,
2240
  • 1,368
  • 1
  • 6
  • 18
  • This is a nice way to go if you're having trouble with file permissions, but it won't quote your fields, so other tools may misinterpret the CSV if your data includes commas or quotation marks. – Paul A Jungwirth Apr 08 '21 at 15:29
13

This is simple, and it works on anything without needing batch mode or output files:

select concat_ws(',',
    concat('"', replace(field1, '"', '""'), '"'),
    concat('"', replace(field2, '"', '""'), '"'),
    concat('"', replace(field3, '"', '""'), '"'))

from your_table where etc;

Explanation:

  1. Replace " with "" in each field --> replace(field1, '"', '""')
  2. Surround each result in quotation marks --> concat('"', result1, '"')
  3. Place a comma between each quoted result --> concat_ws(',', quoted1, quoted2, ...)

That's it!

Michael
  • 5,910
  • 4
  • 52
  • 74
Marty Hirsch
  • 181
  • 1
  • 3
  • 2
    Note that `NULL` values will be skipped by `concat_ws()`, resulting in a column mismatch. To avoid this, simply use an empty string instead: `IFNULL(field, '')` (or whatever else you use to represent `NULL`) – Marco Roy May 21 '19 at 23:11
11

I encountered the same problem and Paul's Answer wasn't an option since it was RDS. Replacing the tab with the commas did not work as the data had embedded commas & tabs. I found that the mycli which is a drop-in alternative for the mysql-client supports csv output outof the box with the --csv flag

mycli db_name --csv -e "select * from flowers" > flowers.csv
Shenal Silva
  • 1,565
  • 3
  • 24
  • 35
  • Works like a charm, you can install mycli in macOS via: `brew update && brew install mycli`. I'm never using the stock mysql client again, mycli is so legit! – bithavoc Jul 24 '20 at 02:40
  • 1
    Mycli and it's cousin pgcli are fantastic. I came here to add this tip mysql because I wasn't getting anywhere with the other solutions. Cleanly outputting to a local .csv is surprisingly difficult with mysql. – iturgeon Sep 04 '20 at 19:17
  • Awesome, it's so sad that this answer got very few "likes". It's the only solution that fits my needs. All other didn't work perfectly for me but this. I had line breaks in columns - and that led to new lines in Excel program when opening the file. – Skiff Nov 20 '20 at 23:57
  • This answer won't work if you're stuck using version 1.8.1 (maybe because you're using an older OS that still has Python 2.x), as '--csv' wasn't available in that version. – Joseph Van Riper Dec 14 '20 at 09:35
10

Alternatively to the answer above, you can have a MySQL table that uses the CSV engine.

Then you will have a file on your hard disk that will always be in a CSV format which you could just copy without processing it.

Jonathan
  • 1,973
  • 4
  • 18
  • 25
9

To expand on previous answers, the following one-liner exports a single table as a tab-separated file. It's suitable for automation, exporting the database every day or so.

mysql -B -D mydatabase -e 'select * from mytable'

Conveniently, we can use the same technique to list out MySQL's tables, and to describe the fields on a single table:

mysql -B -D mydatabase -e 'show tables'

mysql -B -D mydatabase -e 'desc users'

Field   Type    Null    Key Default Extra
id  int(11) NO  PRI NULL    auto_increment
email   varchar(128)    NO  UNI NULL    
lastName    varchar(100)    YES     NULL    
title   varchar(128)    YES UNI NULL    
userName    varchar(128)    YES UNI NULL    
firstName   varchar(100)    YES     NULL    
johntellsall
  • 11,853
  • 3
  • 37
  • 32
  • 4
    To convert to CSV: `mysql -B -D mydatabase -e 'select * from mytable' | sed -e 's/\t/,/g'` – DSimon Jan 12 '15 at 21:15
  • 7
    Using `sed -e 's/\t/,/g'` is only safe if you are sure that your data doesn't contain any commas or tabs. – awatts Oct 20 '15 at 16:23
8

Building on user7610, here is the best way to do it. With mysql outfile there were 60 mins of file ownership and overwriting problems.

It's not cool, but it worked in 5 mins.

php csvdump.php localhost root password database tablename > whatever-you-like.csv

<?php

$server = $argv[1];
$user = $argv[2];
$password = $argv[3];
$db = $argv[4];
$table = $argv[5];

mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

// fetch the data
$rows = mysql_query('SELECT * FROM ' . $table);
$rows || die(mysql_error());


// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings

$fields = [];
for($i = 0; $i < mysql_num_fields($rows); $i++) {
    $field_info = mysql_fetch_field($rows, $i);
    $fields[] = $field_info->name;
}
fputcsv($output, $fields);

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

?>
Michael Cole
  • 13,473
  • 4
  • 66
  • 81
7

Here's what I do:

echo $QUERY | \
  mysql -B  $MYSQL_OPTS | \
  perl -F"\t" -lane 'print join ",", map {s/"/""/g; /^[\d.]+$/ ? $_ : qq("$_")} @F ' | \
  mail -s 'report' person@address

The perl script (sniped from elsewhere) does a nice job of converting the tab spaced fields to CSV.

Mei
  • 1,059
  • 1
  • 11
  • 19
extraplanetary
  • 184
  • 1
  • 2
  • 2
    This is great. Slight improvement might be to quote everything except numbers `perl -F"\t" -lane 'print join ",", map {s/"/""/g; /^\d+(?:\.\d+)?$/ ? $_ : qq("$_")} @F '` -- yours would not quote `1.2.3` – artfulrobot Mar 15 '17 at 16:00
  • 1
    This should be the accepted solution IMHO, with @artfulrobot's improvement of course. – chrisinmtown Sep 25 '20 at 14:55
5

Not exactly as a CSV format, but tee command from MySQL client can be used to save the output into a local file:

tee foobar.txt
SELECT foo FROM bar;

You can disable it using notee.

The problem with SELECT … INTO OUTFILE …; is that it requires permission to write files at the server.

Denilson Sá Maia
  • 40,640
  • 31
  • 100
  • 109
  • If .csv extension is used instead of .txt, are there any formatting issues to be aware of? – datalifenyc May 08 '18 at 21:53
  • @myidealab Formatting issues arise from commas etc. not being escaped. CSV is a plain text format so there's no formatting issue just from swapping out the extension. – jkmartindale Jun 20 '19 at 17:24
4

What worked for me:

SELECT *
FROM students
WHERE foo = 'bar'
LIMIT 0,1200000
INTO OUTFILE './students-1200000.csv'
FIELDS TERMINATED BY ',' ESCAPED BY '"'
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';

None of the solutions on this thread worked for my particular case, I had pretty json data inside one of the columns, which would get messed up in my csv output. For those with a similar problem, try lines terminated by \r\n instead.

Also another problem for those trying to open the csv with Microsoft Excel, keep in mind there is a limit of 32,767 characters that a single cell can hold, above that it overflows to the rows below. To identify which records in a column have the issue, use the query below. You can then truncate those records or handle them as you'd like.

SELECT id,name,CHAR_LENGTH(json_student_description) AS 'character length'
FROM students
WHERE CHAR_LENGTH(json_student_description)>32767;
Rohit Chemburkar
  • 959
  • 6
  • 13
3

Using the solution posted by Tim, I created this bash script to facilitate the process (root password is requested, but you can modify the script easily to ask for any other user):

#!/bin/bash

if [ "$1" == "" ];then
    echo "Usage: $0 DATABASE TABLE [MYSQL EXTRA COMMANDS]"
    exit
fi

DBNAME=$1
TABLE=$2
FNAME=$1.$2.csv
MCOMM=$3

echo "MySQL password:"
stty -echo
read PASS
stty echo

mysql -uroot -p$PASS $MCOMM $DBNAME -B -e "SELECT * FROM $TABLE;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > $FNAME

It will create a file named: database.table.csv

lepe
  • 22,543
  • 9
  • 85
  • 99
  • what about embedded double-quote characters? They need to be escaped also but I don't see a pattern in the sed incantation for them. – chrisinmtown Sep 25 '20 at 14:50
3

If you have PHP set up on the server, you can use mysql2csv to export an (actually valid) CSV file for an abitrary mysql query. See my answer at MySQL - SELECT * INTO OUTFILE LOCAL ? for a little more context/info.

I tried to maintain the option names from mysql so it should be sufficient to provide the --file and --query options:

./mysql2csv --file="/tmp/result.csv" --query='SELECT 1 as foo, 2 as bar;' --user="username" --password="password"

"Install" mysql2csv via

wget https://gist.githubusercontent.com/paslandau/37bf787eab1b84fc7ae679d1823cf401/raw/29a48bb0a43f6750858e1ddec054d3552f3cbc45/mysql2csv -O mysql2csv -q && (sha256sum mysql2csv | cmp <(echo "b109535b29733bd596ecc8608e008732e617e97906f119c66dd7cf6ab2865a65  mysql2csv") || (echo "ERROR comparing hash, Found:" ;sha256sum mysql2csv) ) && chmod +x mysql2csv

(download content of the gist, check checksum and make it executable).

Hirnhamster
  • 6,032
  • 7
  • 35
  • 65
2

The following produces tab-delimited and valid CSV output. Unlike most of the other answers, this technique correctly handles escaping of tabs, commas, quotes, and new lines without any stream filter like sed, awk, or tr. The example shows how to pipe a remote mysql table directly into a local sqlite database using streams. This works without FILE permission or SELECT INTO OUTFILE permission. I have added new lines for readability.

mysql -B -C --raw -u 'username' --password='password' --host='hostname' 'databasename'
-e 'SELECT
    CONCAT('\''"'\'',REPLACE(`id`,'\''"'\'', '\''""'\''),'\''"'\'') AS '\''id'\'',
    CONCAT('\''"'\'',REPLACE(`value`,'\''"'\'', '\''""'\''),'\''"'\'') AS '\''value'\''
    FROM sampledata'
2>/dev/null | sqlite3 -csv -separator $'\t' mydb.db '.import /dev/stdin mycsvtable'

The 2>/dev/null is needed to suppress the warning about the password on the command line.

If your data has NULLs, you can use the IFNULL() function in the query.

humbads
  • 2,737
  • 1
  • 22
  • 21
2

If you are getting an error of secure-file-priv then, also after shifting your destination file location inside the C:\ProgramData\MySQL\MySQL Server 8.0\Uploads and also after then the query-

SELECT * FROM attendance INTO OUTFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\FileName.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

is not working, you have to just change \(backsplash) from the query to / (forwardsplash)

And that works !!

Example:

SELECT * FROM attendance INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/FileName.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Each time when you run the successful query, it will generate the new csv file each time! Cool Right?

AAYUSH SHAH
  • 95
  • 1
  • 6
2

In my case from table_name ..... before INTO OUTFILE ..... gives an error(Unexpected ordering of clauses. (near "FROM" at position 10)).

What works for me.

SELECT *
INTO OUTFILE '/Volumes/Development/sql/sql/enabled_contacts.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n' 
FROM table_name
WHERE column_name = 'value'
Md. Robi Ullah
  • 571
  • 4
  • 20
1

Tiny bash script for doing simple query to CSV dumps, inspired by https://stackoverflow.com/a/5395421/2841607.

#!/bin/bash

# $1 = query to execute
# $2 = outfile
# $3 = mysql database name
# $4 = mysql username

if [ -z "$1" ]; then
    echo "Query not given"
    exit 1
fi

if [ -z "$2" ]; then
    echo "Outfile not given"
    exit 1
fi

MYSQL_DB=""
MYSQL_USER="root"

if [ ! -z "$3" ]; then
    MYSQL_DB=$3
fi

if [ ! -z "$4" ]; then
    MYSQL_USER=$4
fi

if [ -z "$MYSQL_DB" ]; then
    echo "Database name not given"
    exit 1
fi

if [ -z "$MYSQL_USER" ]; then
    echo "Database user not given"
    exit 1
fi

mysql -u $MYSQL_USER -p -D $MYSQL_DB -B -s -e "$1" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > $2
echo "Written to $2"
minitauros
  • 1,588
  • 14
  • 20
  • This uses the sed incantation that ignores embedded double-quote characters. Suggest using the perl solution instead. – chrisinmtown Sep 25 '20 at 14:52
1

The following bash script works for me. It optionally also gets the schema for the requested tables.

#!/bin/bash
#
# export mysql data to CSV
#https://stackoverflow.com/questions/356578/how-to-output-mysql-query-results-in-csv-format
#

#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'

#
# a colored message 
#   params:
#     1: l_color - the color of the message
#     2: l_msg - the message to display
#
color_msg() {
  local l_color="$1"
  local l_msg="$2"
  echo -e "${l_color}$l_msg${endColor}"
}


#
# error
#
# show the given error message on stderr and exit
#
#   params:
#     1: l_msg - the error message to display
#
error() {
  local l_msg="$1"
  # use ansi red for error
  color_msg $red "Error:" 1>&2
  color_msg $red "\t$l_msg" 1>&2
  usage
}

#
# display usage 
#
usage() {
  echo "usage: $0 [-h|--help]" 1>&2
  echo "               -o  | --output      csvdirectory"    1>&2
  echo "               -d  | --database    database"   1>&2
  echo "               -t  | --tables      tables"     1>&2
  echo "               -p  | --password    password"   1>&2
  echo "               -u  | --user        user"       1>&2
  echo "               -hs | --host        host"       1>&2
  echo "               -gs | --get-schema"             1>&2
  echo "" 1>&2
  echo "     output: output csv directory to export mysql data into" 1>&2
  echo "" 1>&2
  echo "         user: mysql user" 1>&2
  echo "     password: mysql password" 1>&2
  echo "" 1>&2
  echo "     database: target database" 1>&2
  echo "       tables: tables to export" 1>&2
  echo "         host: host of target database" 1>&2
  echo "" 1>&2
  echo "  -h|--help: show help" 1>&2
  exit 1
}

#
# show help 
#
help() {
  echo "$0 Help" 1>&2
  echo "===========" 1>&2
  echo "$0 exports a csv file from a mysql database optionally limiting to a list of tables" 1>&2
  echo "   example: $0 --database=cms --user=scott --password=tiger  --tables=person --output person.csv" 1>&2
  echo "" 1>&2
  usage
}

domysql() {
  mysql --host $host -u$user --password=$password $database
}

getcolumns() {
  local l_table="$1"
  echo "describe $l_table" | domysql | cut -f1 | grep -v "Field" | grep -v "Warning" | paste -sd "," - 2>/dev/null
}

host="localhost"
mysqlfiles="/var/lib/mysql-files/"

# parse command line options
while true; do
  #echo "option $1"
  case "$1" in
    # options without arguments
    -h|--help) usage;;
    -d|--database)     database="$2" ; shift ;;
    -t|--tables)       tables="$2" ; shift ;;
    -o|--output)       csvoutput="$2" ; shift ;;
    -u|--user)         user="$2" ; shift ;;
    -hs|--host)        host="$2" ; shift ;;
    -p|--password)     password="$2" ; shift ;;
    -gs|--get-schema)  option="getschema";; 
    (--) shift; break;;
    (-*) echo "$0: error - unrecognized option $1" 1>&2; usage;;
    (*) break;;
  esac
  shift
done

# checks
if [ "$csvoutput" == "" ]
then
  error "ouput csv directory not set"
fi
if [ "$database" == "" ]
then
  error "mysql database not set"
fi
if [ "$user" == "" ]
then
  error "mysql user not set"
fi
if [ "$password" == "" ]
then
  error "mysql password not set"
fi

color_msg $blue "exporting tables of database $database"
if [ "$tables" = "" ]
then
tables=$(echo "show tables" | domysql)
fi

case $option in
  getschema) 
   rm $csvoutput$database.schema
   for table in $tables
   do
     color_msg $blue "getting schema for $table"
     echo -n "$table:" >> $csvoutput$database.schema
     getcolumns $table >> $csvoutput$database.schema
   done  
   ;;
  *)
for table in $tables
do
  color_msg $blue "exporting table $table"
  cols=$(grep "$table:" $csvoutput$database.schema | cut -f2 -d:)
  if [  "$cols" = "" ]
  then
    cols=$(getcolumns $table)
  fi
  ssh $host rm $mysqlfiles/$table.csv
cat <<EOF | mysql --host $host -u$user --password=$password $database 
SELECT $cols FROM $table INTO OUTFILE '$mysqlfiles$table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
EOF
  scp $host:$mysqlfiles/$table.csv $csvoutput$table.csv.raw
  (echo "$cols"; cat $csvoutput$table.csv.raw) > $csvoutput$table.csv
  rm $csvoutput$table.csv.raw
done
  ;;
esac
Wolfgang Fahl
  • 12,097
  • 9
  • 75
  • 150
1

Standing on the shoulders of @ChrisJohnson, I extended the answer from Feb 2016 with a custom dialect for reading. This shell pipeline tool does not need to connect to your database, handles random commas and quotes in the input, and works nicely in Python2 and Python3!

#!/usr/bin/env python
import csv
import sys
# fields are separated by tabs; double-quotes may occur anywhere
csv.register_dialect("mysql", delimiter="\t", quoting=csv.QUOTE_NONE)
tab_in = csv.reader(sys.stdin, dialect="mysql")
comma_out = csv.writer(sys.stdout, dialect=csv.excel)
for row in tab_in:
    # print("row: {}".format(row))
    comma_out.writerow(row)

Use that print statement to convince yourself it's parsing your input correctly :)

A major caveat: treatment of carriage return characters, ^M aka control-M, \r in linux terms. Altho batch-mode Mysql output correctly escapes embedded newline characters, so there is truly one row per line (defined by linux newline character \n), mysql puts no quotes around column data. If a data item has an embedded carriage-return character, csv.reader rejects that input with this exception:

new-line character seen in unquoted field - 
do you need to open the file in universal-newline mode?

Please don't @ me saying I should use universal file mode by re-opening sys.stdin.fileno with mode 'rU'. I tried that, it causes the embedded \r characters to be treated as end-of-record markers, so a single input record is incorrectly transformed into many incomplete output records. I have not found a Python solution to this limitation of Python's csv.reader module. I think the root cause is the csv.reader implementation/limitation noted in their documentation https://docs.python.org/3/library/csv.html#csv.reader:

The reader is hard-coded to recognise either '\r' or '\n' as end-of-line,
and ignores lineterminator.

The weak & unsatisfying solution I can offer is to change each \r character to the two-character sequence '\n' before Python's csv.reader sees the data. I used the sed command. Here's an example of a pipeline with a mysql select and the python script from above:

mysql -u user db --execute="select * from table where id=12345" \
  | sed -e 's/\r/\\n/g' \
  | mysqlTsvToCsv.py

After fighting this for some time I think Python is not the right solution. If you can live with perl, I think the one-liner script offered by @artfulrobot may be the most-effective and simplest solution.

chrisinmtown
  • 2,253
  • 23
  • 35
0

Try this code:

SELECT 'Column1', 'Column2', 'Column3', 'Column4', 'Column5'
UNION ALL
SELECT column1, column2,
column3 , column4, column5 FROM demo
INTO OUTFILE '/tmp/demo.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

For more information: http://dev.mysql.com/doc/refman/5.1/en/select-into.html

trungduc
  • 11,323
  • 4
  • 29
  • 51
Indrajeet Singh
  • 2,717
  • 21
  • 24
  • Regard same if i run following query i am getting error. My query : https://justpaste.it/2fp6f Any help thanks. @Indrajeet Singh – zus Feb 19 '20 at 05:34
0

This solution places the SQL query in a heredoc and pipes the output though a filter:

$cat query.sh 
#!/bin/bash

mysql --defaults-group-suffix=[DATABASE_NAME] --batch << EOF | python query.py
SELECT [FIELDS]
FROM [TABLE]
EOF

This version of the python filter works without requiring the use of the csv module:

$cat query.py 
import sys

for line in sys.stdin:
    print(','.join(["\"" + str(element) + "\"" for element in line.rstrip('\n').split('\t')]))

This version of the python filter uses the csv module and involves slightly more code but is arguably a little bit more clear:

$cat query.py 
import csv, sys

csv_reader = csv.reader(sys.stdin, delimiter='\t')
csv_writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONNUMERIC)

for line in csv_reader:
    csv_writer.writerow(line)

Or you could use pandas:

$cat query.py 
import csv, sys
import pandas as pd

df = pd.read_csv(sys.stdin, sep='\t')
df.to_csv(sys.stdout, index=False, quoting=csv.QUOTE_NONNUMERIC)
Alex Ryan
  • 3,181
  • 5
  • 20
  • 36
0

For those, who may want to download query result in CSV format but doesn't have access the server file but the database. First of all, it's not linux command. Steps are bellow:

  1. Create a view with the query. For example: (Create VIEW v as (Select * from user where status = 0))
  2. The view will be created under the view section of your database.
  3. Now export the view as CSV.
  4. If you need the table column as header of CSV, set Export method: to Custom - display all possible options and check Put columns names in the first row.
Sudarshan
  • 765
  • 11
  • 25
0

If you are getting this error while you try to export your file

ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

and you are not able to solve this error. You can do one thing by simply running this python script

import mysql.connector
import csv

con = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="Your Password"
)

cur = con.cursor()

cur.execute("USE DbName")
cur.execute("""
select col1,col2 from table
where <cond>
""")

with open('Filename.csv',mode='w') as data:
    fieldnames=["Field1","Field2"]
    writer=csv.DictWriter(data,fieldnames=fieldnames)
    writer.writeheader()
    for i in cur:
        writer.writerow({'Field1':i[0],'Field2':i[1]})
Pranav
  • 57
  • 4
  • Welcome to Stack Overflow! This doesn't answer the question. Please review https://stackoverflow.com/help/how-to-answer. – stephenwade Apr 04 '20 at 16:25
0

Yet another solution...

This one avoids having to write output to a file, only requires expat to be installed, properly escapes values, and outputs empty string (instead of a literal NULL) for null values.

You tell MySQL to output the results in XML format (using the --xml flag), and then pipe the results through the C program below.

This should also be pretty close to the fastest possible way to do this.


// mysql-xml-to-csv.c

#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <expat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/*
    Example of MySQL XML output:

    <?xml version="1.0"?>

    <resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" statement="SELECT id as IdNum, lastName, firstName FROM User">
        <row>
            <field name="IdNum">100040</field>
            <field name="lastName" xsi:nil="true"/>
            <field name="firsttName">Cher</field>
        </row>
    </resultset>
*/

#define BUFFER_SIZE     (1 << 16)

// These accumulate the first row column names and values until first row is entirely read (unless the "-N" flag is given)
static XML_Char **column_names;
static size_t num_column_names;
static XML_Char **first_row_values;
static size_t num_first_row_values;

// This accumulates one column's value
static XML_Char *elem_text;                     // note: not nul-terminated
static size_t elem_text_len;

// Flags
static int first_column;
static int reading_value;

// Expat callback functions
static void handle_elem_start(void *data, const XML_Char *el, const XML_Char **attr);
static void handle_elem_text(void *userData, const XML_Char *s, int len);
static void handle_elem_end(void *data, const XML_Char *el);

// Helper functions
static void output_csv_row(XML_Char **values, size_t num);
static void output_csv_text(const char *s, size_t len);
static void add_string(XML_Char ***arrayp, size_t *lengthp, const XML_Char *string, size_t len);
static void add_chars(XML_Char **strp, size_t *lenp, const XML_Char *string, size_t nchars);
static size_t xml_strlen(const XML_Char *string);
static void free_strings(XML_Char ***arrayp, size_t *lengthp);
static void usage(void);

int
main(int argc, char **argv)
{
    char buf[BUFFER_SIZE];
    int want_column_names = 1;
    XML_Parser p;
    FILE *fp;
    size_t r;
    int i;

    // Parse command line
    while ((i = getopt(argc, argv, "hN")) != -1) {
        switch (i) {
        case 'N':
            want_column_names = 0;
            break;
        case 'h':
            usage();
            exit(0);
        case '?':
        default:
            usage();
            exit(1);
        }
    }
    argv += optind;
    argc -= optind;
    switch (argc) {
    case 0:
        fp = stdin;
        break;
    case 1:
        if ((fp = fopen(argv[0], "r")) == NULL)
            err(1, "%s", argv[0]);
        break;
    default:
        usage();
        exit(1);
    }

    // Initialize arrays for column names and first row values
    if (want_column_names) {
        if ((column_names = malloc(10 * sizeof(*column_names))) == NULL)
            err(1, "malloc");
        if ((first_row_values = malloc(10 * sizeof(*first_row_values))) == NULL)
            err(1, "malloc");
    }

    // Initialize parser
    if ((p = XML_ParserCreate(NULL)) == NULL)
        errx(1, "can't initialize parser");
    XML_SetElementHandler(p, handle_elem_start, handle_elem_end);
    XML_SetCharacterDataHandler(p, handle_elem_text);

    // Process file
    while (1) {
        if ((r = fread(buf, 1, sizeof(buf), fp)) == 0 && ferror(fp))
            errx(1, "error reading input");
        if (XML_Parse(p, buf, r, r == 0) == XML_STATUS_ERROR)
            errx(1, "line %u: %s", (unsigned int)XML_GetCurrentLineNumber(p), XML_ErrorString(XML_GetErrorCode(p)));
        if (r == 0)
            break;
    }

    // Clean up
    XML_ParserFree(p);
    fclose(fp);

    // Done
    return 0;
}

static void
handle_elem_start(void *data, const XML_Char *name, const XML_Char **attr)
{
    if (strcmp(name, "row") == 0)
        first_column = 1;
    else if (strcmp(name, "field") == 0) {
        if (column_names != NULL) {
            while (*attr != NULL && strcmp(*attr, "name") != 0)
                attr += 2;
            if (*attr == NULL)
                errx(1, "\"field\" element is missing \"name\" attribute");
            add_string(&column_names, &num_column_names, attr[1], xml_strlen(attr[1]));
        } else {
            if (!first_column)
                putchar(',');
            putchar('"');
        }
        reading_value = 1;
    }
}

static void
handle_elem_text(void *userData, const XML_Char *s, int len)
{
    if (!reading_value)
        return;
    if (column_names != NULL)
        add_chars(&elem_text, &elem_text_len, s, len);
    else
        output_csv_text(s, len);
}

static void
handle_elem_end(void *data, const XML_Char *name)
{
    if (strcmp(name, "row") == 0) {
        if (column_names != NULL) {
            output_csv_row(column_names, num_column_names);
            output_csv_row(first_row_values, num_first_row_values);
            free_strings(&column_names, &num_column_names);
            free_strings(&first_row_values, &num_first_row_values);
        } else
            putchar('\n');
    } else if (strcmp(name, "field") == 0) {
        if (column_names != NULL) {
            add_string(&first_row_values, &num_first_row_values, elem_text, elem_text_len);
            free(elem_text);
            elem_text = NULL;
            elem_text_len = 0;
        } else
            putchar('"');
        first_column = 0;
        reading_value = 0;
    }
}

static void
output_csv_row(XML_Char **values, size_t num_columns)
{
    int i;

    for (i = 0; i < num_columns; i++) {
        if (i > 0)
            putchar(',');
        putchar('"');
        output_csv_text(values[i], xml_strlen(values[i]));
        putchar('"');
    }
    putchar('\n');
}

static void
output_csv_text(const XML_Char *s, size_t len)
{
    while (len-- > 0) {
        if (*s == '"')
            putchar('"');
        putchar(*s);
        s++;
    }
}

static void
add_string(XML_Char ***arrayp, size_t *lengthp, const XML_Char *string, size_t nchars)
{
    char **new_array;

    if ((new_array = realloc(*arrayp, (*lengthp + 1) * sizeof(**arrayp))) == NULL)
        err(1, "malloc");
    *arrayp = new_array;
    if (((*arrayp)[*lengthp] = malloc((nchars + 1) * sizeof(XML_Char))) == NULL)
        err(1, "malloc");
    memcpy((*arrayp)[*lengthp], string, nchars * sizeof(XML_Char));
    (*arrayp)[*lengthp][nchars] = (XML_Char)0;
    (*lengthp)++;
}

static void
add_chars(XML_Char **strp, size_t *lenp, const XML_Char *string, size_t nchars)
{
    XML_Char *new_array;

    if ((new_array = realloc(*strp, (*lenp + nchars) * sizeof(XML_Char))) == NULL)
        err(1, "malloc");
    *strp = new_array;
    memcpy(*strp + *lenp, string, nchars * sizeof(XML_Char));
    *lenp += nchars;
}

static size_t
xml_strlen(const XML_Char *string)
{
    size_t len;

    len = 0;
    while (string[len] != (XML_Char)0)
        len++;
    return len;
}

static void
free_strings(char ***arrayp, size_t *lengthp)
{
    while (*lengthp > 0)
        free((*arrayp)[--*lengthp]);
    free(*arrayp);
    *arrayp = NULL;
}

static void
usage(void)
{
    fprintf(stderr, "Usage: mysql-xml-to-csv [options] [file.xml]\n");
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "  -N\tDo not output column names as the first row\n");
    fprintf(stderr, "  -h\tShow this usage info\n");
}
Archie
  • 4,680
  • 1
  • 26
  • 35
0

If there is PHP installed on the machine you are using, you can write a PHP script to do that. It requires the PHP installation has the MySQL extension installed.

You can call the PHP interpreter from the command line like so:

php --php-ini path/to/php.ini your-script.php

I am including the --php-ini switch, because you may need to use your own PHP configuration that enables the MySQL extension. On PHP 5.3.0+ that extension is enabled by default, so that is no longer necessary to use the configuration to enable it.

Then you can write your export script like any normal PHP script:

<?php
    #mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("mydb") or die(mysql_error());

    $result = mysql_query("SELECT * FROM table_with_the_data p WHERE p.type = $typeiwant");

    $result || die(mysql_error());

    while($row = mysql_fetch_row($result)) {
      $comma = false;
      foreach ($row as $item) {

        # Make it comma separated
        if ($comma) {
          echo ',';
        } else {
          $comma = true;
        }

        # Quote the quotes
        $quoted = str_replace("\"", "\"\"", $item);

        # Quote the string
        echo "\"$quoted\"";
      }
        echo "\n";
    }
?>

The advantage of this method is, that it has no problems with varchar and text fields, that have text containing newlines. Those fields are correctly quoted and those newlines in them will be interpreted by the CSV reader as a part of the text, not record separators. That is something that is hard to correct afterwards with sed or so.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user7610
  • 17,196
  • 7
  • 97
  • 120
  • 1
    Far from being unnecessarily complicated, this is the only solution here that goes in a direction likely to be correct - albeit with a bit more work. CSV is more complex than it first appears, and you've made a variety of mistakes. eg backslashes in the original. Better to use a library which has worked out all the issues for writing to CSV. – mc0e Oct 10 '13 at 08:57
  • @mc0e Usually you either double the quote or escape the quote. I decided to double the quote, therefore I do not need an escape character. Different software has different ideas about CSV details. For example, LOAD DATA in MySQL indeed treats \ as escape character, whereas Open Office Calc does not. When I wrote the answer I was exporting data into a spreadsheet. – user7610 Nov 02 '15 at 12:51
  • 1
    My code handles everything that was needed to export _my_ dataset back in the day ;) Your answer is also a step in the correct direction, but as the first comment there says, it should 1) connect to the sql database from the script directly as well as 2) use a proper csv library. – user7610 Oct 22 '18 at 09:04
-3

You can use below command from your SQL editor/Terminal:

"mysql -h(hostname/IP>) -u(username) -p(password) databasename <(query.sql) > outputFILE(.txt/.xls)"

e.g hostname -x.x.x.x

uname - username

password - password

DBName - employeeDB

queryFile - employee.sql

outputFile - outputFile.xls

mysql -hx.x.x.x -uusername -ppassword employeeDB< employee.sql> outputFile.xls

Make sure you are executing the command from the directory where SQL query is located or mention the full path of the sql query location in the above command.

Ripudaman Singh
  • 305
  • 2
  • 8
-4

If you are on production or any other server with no access to file system, you can use this simple trick and a little bit of manual effort to get what you want.

Step 1. Just wrap all the columns under CONCAT and use as CSVFormat option provided by MySQL to get comma-delimited results. (or use any delimiter you want). Here is an example:

SELECT 
    CONCAT(u.id,
            ',',
            given,
            ',',
            family,
            ',',
            email,
            ',',
            phone,
            ',',
            ua.street_number,
            ',',
            ua.route,
            ',',
            ua.locality,
            ',',
            ua.state,
            ',',
            ua.country,
            ',',
            ua.latitude,
            ',',
            ua.longitude) AS CSVFormat
FROM
    table1 u
        LEFT JOIN
    table2 ua ON u.address_id = ua.id
WHERE
    role_policy = 31 and is_active = 1;

Step 2. Copy results from your terminal to a file and clean up all the pipe characters (that forms the layout of your results) using any text editor.

Step 3. Save as .csv file and that's it.

saran3h
  • 7,930
  • 2
  • 23
  • 33
  • This solution is error-prone and not recommended. Instead, use one of MySQL's built-in methods of extracting a valid CSV output. – MAbraham1 Feb 07 '20 at 01:49