MySql command line: execute two queries at once?
Wouldn't the ";" do the trick?
mysql -h <host> -r <username> -D <database> -e "QUERY1;QUERY2"
Hunter McMillen
Updated on June 04, 2022Comments
-
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 almost 13 yearsHave you tried seperating them with a semi-colon?
-
Devart almost 13 yearsYou could execute a script file - shell> mysql <params> <file.sql>
-
-
Hunter McMillen almost 13 yearsThis worked great! Thanks. I read through the documentation for the command line tool and didn't see ';' marked as a separator.
-
Binus almost 13 yearsIt 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 almost 13 yearsI 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.