Tuesday, March 15, 2011

Can you use batches of SQL in mySQL the way you can with SQL Server?

With SQL Server, I can send

SELECT * FROM FOO
SELECT * FROM BAR

to the server and get back the results of both, all in one trip.

Can I do that with mySQL also?

From stackoverflow
  • As long as the queries have the same number of columns you can do a UNION on the two queries, e.g.

    SELECT * FROM foo 
    UNION
    SELECT * FROM bar
    
    Kristen : Corey Tragers syntax, in SQL Server, would return two recordsets - quite probably with different columns in each - so in the application you could first do a NextRecord loop to process the first recordset, and then a NextRecordSet to get to the second.
  • I can only speak about the mysqli-extension for PHP, but I guess the same will be possible with most mysql-libraries. In PHP, you can send multiple queries, like

    SELECT * FROM foo; SELECT * FROM bar;
    

    with mysqli_multi_query() and iterate through the result-sets with mysqli_next_result().

0 comments:

Post a Comment