Comments - User-Defined Variables

5 years, 2 months ago Dean Trower
This comment has the status of 'removed' and can only be seen by you.

Beware order of evaluation of rows when sorting. It would appear (at least as far as I've tested it) that when statements of the form:

SELECT @var:=<expression> FROM...

are executed, @var will always correspond to the value in the last row returned, as you might expect.

But if you do something like:

SELECT IFNULL(@var:=Name,'unknown') FROM Customers ORDER BY 1 LIMIT 20

The statement containing the variable assignment will be executed *before* the order-by. In this case, the value left in @var at the end might not even correspond to any of the rows returned!

It would be good if the MariaDB docs could clarify under exactly what conditions the order-of-variable-assignments corresponds to the order-of-returned rows, and especially under what conditions this differs!

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.