MySql command line: execute two queries at once?

13,110

Wouldn't the ";" do the trick?

mysql -h <host> -r <username> -D <database> -e "QUERY1;QUERY2"

Share:
13,110
Hunter McMillen
Author by

Hunter McMillen

Updated on June 04, 2022

Comments

  • Hunter McMillen
    Hunter McMillen almost 2 years

    I am using the MySql command tool to query a database from inside a Ruby script I wrote. However the need has arisen for a query that creates a temporary table, then in another query I need to perform a join on the temporary table. The only problem is every time I invoke mysql -h <host> -r <username> -D <database> -e "QUERY" I get a new transaction, so when I am trying to perform my join, the temporary table doesn't exist anymore.

    Is there anyway to do execute two separate queries in one invocation of the mysql command line tool??

    Something like: mysql -h <host> -r <username> -D <database> -e "QUERY1" -e "QUERY2"

    Or is there some alternative way to store my queries? like in a text file or something?

    Thanks

    • Jacob
      Jacob almost 13 years
      Have you tried seperating them with a semi-colon?
    • Devart
      Devart almost 13 years
      You could execute a script file - shell> mysql <params> <file.sql>
  • Hunter McMillen
    Hunter McMillen almost 13 years
    This worked great! Thanks. I read through the documentation for the command line tool and didn't see ';' marked as a separator.
  • Binus
    Binus almost 13 years
    It was a lucky guess ;). Also if you are about to query the temporary table multiple times and you care about performance, it might be better to create a connection to the mysql. Then you can create the table just once and execute all the queries in one session.
  • Hunter McMillen
    Hunter McMillen almost 13 years
    I just need it for a single small query, and I had to create a temp table because it isn't my database :( Thanks for your suggestions.