Permittivity Permittivity - 5 months ago 14
SQL Question

Use selection from previous mysql-statement

Basically I'm writing a bash script that is supposed to select multiple values from 2 mysql tables.

mysql -h "" "database1" << EOF
SET @user='testuser';
SELECT serverId FROM member WHERE username = @user;
SELECT slots FROM server WHERE id = @serverId;

I set the variable for @user for testing purposes, normally it's given by a php script.

I just don't know how to use the result I get from the first SELECT statement ( the serverId ) to be used in the second SELECT statement in order to find out the slots this server contains.


In your case, you could just do it in one SQL SELECT:

slots=$(mysql -uuser -ppassword -Ddatabase -N -e "SELECT slots FROM server,member WHERE AND member.username=$user;")

You might want to play with bash arrays to parse results.

If that's true you can make multiple mysql-client calls to do that, keep in mind it's always better to make your DBMS perform the joining/sorting/formatting instead of your own code.