Can I send 2 MySql queries' results to a csv file?

Hi everyone,
A code that contains 2 quries: first one lists hours made for a company (company1) and second: total of those hours, writes the results in a csv file goes like this:


<?php
   // Connect to database server
   $con=mysqli_connect("localhost","root","root","db");
   if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql1 = "SELECT startDate,endDate,intervalTime, institution, company
   INTO OUTFILE 'd:/companies/xxx.csv'
   FIELDS TERMINATED BY ','
   LINES TERMINATED BY '\
'
   FROM june2013
   WHERE company='company1'";

   $sql2 = "SELECT company, SEC_TO_TIME(SUM(TIME_TO_SEC(intervalTime)))
     INTO OUTFILE 'd:/companies/xxx.csv'
      FIELDS TERMINATED BY ','
      FROM june2013 GROUP BY company
      WHERE COMPANY='company1'";

  if (mysqli_query($con,$sql1))
    {
    echo "List added to text file successfully";
    }
    else
{
     echo "Error transferring list " . mysqli_error($con);
    }
  if (mysqli_query($con,$sql2))
    {
     echo "Summary added to text file successfully";
    }
    else
   {
     echo "Error transferring summary " . mysqli_error($con);
    }
  mysqli_close($con);
?>

If I run 1 query only (just sql1 for example), it goes well. If I add a second query (sql2) I get an error message that says:
Error transferring summary File ‘d:/companies/xxx.csv’ already existsError transferring summary

Is there a way to send 2 queries’ results to a csv file?
Thanks a lot!

why not create 2 csv files? they have different content anyway

The csv file contains a list of hours worked in a company and at the end a row with total hours. Why should I separate it ?!
If sending 2 queries to one csv is impossible I thought I might create a temporary table with the 1st query’s result and then add it a row with “total” query result. I’ll need to know how to create that temporary table but before doing so I wanted to make sure I can’t send 2 queries to a csv file.

i assure you, i am not

you could also open up the first csv in excel, and create a total row using the sum function

but even better would be to combine your two queries into a single UNION query, and output that

Thank you r937,
“UNION” is the magic word. Thanks so much !

let me know if you have any problems with that query

Hi r937
That “UNION” idea you provided me with utterly solved all my problems (for this time being :slight_smile: ). I’m very grateful to you hoping you’ll bother offering an hand for problems to come. I’m just starting with MySql though I used to work with SQLServer 6 years ago. Apparently, much has been forgotten :frowning:
Thanks again