SET @var := SELECT * FROM table_name: is it possible

mysql, php, phpmyadmin, set

Solution

If you are running multiple queries (i.e., you have a `;` in your query), you either need to run it as two separate queries, or use a command like `mysqli_multi_query`. The `mysql_query` command can only run a single query at a time.

mysql_query('SET @VAR := SELECT MAX(first_column) FROM TABLE');
mysql_query('SELECT @var, table.second_column FROM table');

See also this related question.

Problem

I'm trying to write my MySQL query, but I'm stuck on this - I have multiple queries from my php to the SQL database: ``` SET @var := SELECT MAX(first_column) FROM table; SELECT @var, table.second_column FROM table; ``` But it returns SQL error. When I use it in phpmyadmin, it works fine. I've googled and I saw people use "SET @var = MySQL query" only in stored procedures. Is it possible to use it the way I want?

Original source