membender has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I'm wondering if there's a simple way to have mysql execute two (or more) statements with one execute call. The first would just set a temporary variable based on SELECTing something from one table, and the following would use that variable in its query or queries. One can do this in phpmyadmin (and the mysql client, I imagine) relatively straightforwardly, using SET. eg,
SET @t_id:=(SELECT Team_ID FROM Teams WHERE Name='Boston'); SELECT * FROM Games WHERE Team_ID=@t_id AND Date>'2012-05-01'; [plus indefinitely more, separated by semicolons;]
I could have my script retrieve t_id and then place its value into the second SELECT, but it seems like it would be much more efficient to just have mysql do both in the same call, especially since I don't want to do anything else with t_id within the script.
I've tried executing the entire string (concatenating the SET and SELECT commands above), but nothing seems to be returned. I hope I'm missing something simple (and apologies if I am!), but I haven't been able to find anything on this.
Thanks for any help you can offer. It will be very helpful.
UPDATE: I am using prepared queries, many of which are UPDATEs, so I don't need record sets in return. I started a separate thread with more details. Look for mutliple, mysql and execute/executing in the title, or http://perlmonks.org/?node_id=973409. Thank you for your help! (I think Anonymous Monk in Re^3 is on to what the real issue is :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Executing multiple mysql commands in a single execute() call.
by roboticus (Chancellor) on May 30, 2012 at 10:43 UTC | |
|
Re: Executing multiple mysql commands in a single execute() call.
by Corion (Patriarch) on May 30, 2012 at 09:16 UTC | |
by membender (Novice) on May 30, 2012 at 09:29 UTC | |
by Anonymous Monk on May 30, 2012 at 11:42 UTC | |
by bimleshsharma (Beadle) on May 30, 2012 at 12:23 UTC |