title: Export Sql data to .csv and generate separate .csv files per table based on query
body: <p>Currently, I am using Go to access my database. Ideally, I'd like to generate  .csv based on the table's name and export data to those files based on the query.</p>
  
  <p>For example, if I ran: </p>
  
  <pre><code>select t1.*,t2.* from table1 t1 
  inner join table2 t2 on t2.table_1_id = t1.id
  where t1.linking_id = 22
  </code></pre>
  
  <p>I'd like a .csv file generated for both table 1 and table 2 where data from each table would generate and then export into these two generated files with the same names as the table's names. </p>
  
  <p>I know in PHP I can use <code>$fp = fopen(getcwd().'/table1.csv', 'w');
                  fputcsv($fp, $columns);</code>
  to generate the .csv files with the table's row names. But, I don't believe go needs continuous duplication of foreah columns to generate separate .csv files.</p>
  
  <p>I would like some guidance on exporting and generating sql data to .csv files.</p>
  
  <p>Thank you!</p>
  
  <p>My current import set-up is:</p>
  
  <p><code>import (
      "database/sql"
      _ "github.com/go-sql-driver/mysql" 
      "github.com/joho/sqltocsv"
  )</code></p>
  
  <p>Im able to connect to my database without issue, and query my database when initializing the query via <code>rows, _:= db.Query(SELECT * FROM table1 WHERE id = 22)</code></p>
  
  <p>I am able to write the query data to a pre-existing .csv file using <code>err = sqltocsv.WriteFile("results.csv", rows)</code></p>