Executing interactive queries in MySQL (mostly from the GUI)

I mainly use MySQL GUI tools. This allows me to easily see the results in a spreadsheet, as well as make quick edits and bookmark bookmarks frequently. This suits my needs much better than the command line.

I remember when I was doing this in Oracle DBs many years ago, I could put variables in the query itself so that when I run the query, I was prompted for the variable.

eg.

select email from users where login = [VAR]

      

And when you run the query, the system will offer you VAR and you can enter john_smith14

and execute the query. This is really useful for adhoc requests that you run a lot.

Yes, I know shell and command line scripts can do this more easily, but for several reasons other than that, shell scripts are not suitable for me.

+1


source to share


3 answers


Okay, another solution, since it seems that Bill is right (read the comments on my other answer).

In the Params tab in the lower right corner, you can right click on the "Local Params" folder and add a new parameter. Give it a name, for example: "myTest". It is initially assigned a NULL value. Double click on NULL and enter a new value.

You can now access it in your request like this:



SELECT email FROM users WHERE login = :myTest;

To make this persistence between sessions (opening and closing the query browser), just make it a global parameter instead of a local parameter. This works even when restarting the MySQL server.

+1


source


I'm not sure if there is a way to get the GUI tools to prompt for a value, but you can use variables in MySQL.

SET @myVar='john_smith14';
SELECT email FROM users WHERE login = @myVar;

      



It might even suit you better, since you don't have to enter the value of the variable every time.?

0


source


Using prepared statements can be helpful for you in this case.

PREPARE query1 FROM select email from users where login =?

then execute it with a variable:

SET @a = 'john';
EXECUTE query1 USING @a;

This instruction will be present throughout the session and will be removed if disconnected.

This may seem like a lot of overhead, but is useful when reusing the same query with slightly different values.

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

0


source







All Articles