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

I am trying to write my MySQL query, but I am stuck with this - I have several queries from my php to SQL database:

SET @var := SELECT MAX(first_column)
    FROM table;

SELECT @var, 
    table.second_column
FROM table;

But it returns an SQL error. When I use it in phpmyadmin, it works fine. I googled and I saw people use "SET @var = MySQL query" only in stored procedures. Can I use it the way I want?

+3
source share
2 answers

If you are executing multiple requests (i.e. you have one ;in your request), you either need to run it as two separate requests, or use a command like . A command can only run one request at a time. mysqli_multi_query mysql_query

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

. .

+6
SELECT MAX(first_column) INTO @var FROM table; 
SELECT @var,table.second_column FROM table;  
+1

All Articles