title: MySQL: Dynamically export CSV file with headers
body: <p>Is there anyway to export data in MySQL table with column headers?</p>
  
  <p>I find a way to do this by hard-coding the headers into query but if there are about 60 or even 100 columns in a table then it is impossible.</p>
  
  <p>I tried the query below but I can't get the result as the first part of the query return a concatenated string of all headers in the table. It doesn't give me a desired result:</p>
  
  <pre><code>(select concat(group_concat(COLUMN_NAME separator ','), "\n")
      from information_schema.COLUMNS
      where table_name = '&lt;table name&gt;'
      and table_schema = '&lt;DB name&gt;'
      order by ORDINAL_POSITION) 
  union all
  (select * from &lt;table name&gt; into outfile "E:\\test.csv" fields terminated by "," lines terminated by "\n");
  </code></pre>