0

I have a bunch of queries that I would like to dump directly to CSV. Everything works great EXCEPT there is no option to include the column names from the query (not necessarily the same as the underlying table column names). From looking around, it appears that doing a union with the column names is the only way to do what I want. My problem is:

How can I programmatically get the column names from my query?

The column names are not the same as the underlying table, so I cannot use the Information Schema.

Please let me know if you have any ideas or a better way to accomplish what I'm doing!

Thanks!

Tom Rossi
  • 11,604
  • 5
  • 65
  • 96
  • Do you know things about your query in any programmatic way already? If so, it's a lot easier to handle structured "field" information than parse the queries for 'AS ...,' strings, though a regexp might help: "\s+AS\s*([a-zA-Z0-9_])," but I doubt it'll be fast and it might not work with all the queries. –  Mar 29 '12 at 20:04
  • Have a look at: http://stackoverflow.com/questions/10018316/sql-select-csv-file or http://lifeboysays.wordpress.com/2012/06/23/mysql-how-to-export-data-to-csv-with-column-headers/ (google search) – Meetai.com Oct 16 '12 at 12:51

1 Answers1

0

There's an article here:

http://giantdorks.org/alain/export-mysql-data-into-csv-or-psv-files/

That talks about sending SELECT output directly to a file and then modifying the text until it becomes a valid csv file. That seems to support column headers from the select statement.