Recently I've updated a server and switched from MySQL to MariaDB. One query behaves now differently and I do not understand why. Please enlighten me.
This is my current version
mariadb --version
mariadb Ver 15.1 Distrib 10.6.11-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
The actual query is very long, but here is a condensed form of my problem. I need to update a variable test
, which is updated for each row after all rows have been ordered.
The actual update is more complex, but should not matter here:
@stockMass := @stockMass +
CASE
WHEN `u`.`context` = 'purchase' AND `u`.`status` != 'canceled' THEN `u`.`mass`
WHEN `u`.`context` = 'sale' AND `u`.`status` != 'canceled' THEN -`u`.`mass`
WHEN `u`.`context` = 'massAdjustment' THEN `u`.`mass`
WHEN `u`.`context` = 'splitIn' THEN `u`.`mass`
WHEN `u`.`context` = 'splitOut' THEN -`u`.`mass`
ELSE 0
END AS `stock`
SET @test := 0;
SELECT
*,
@test := @test + 1 AS `test`
FROM (
SELECT
`g_sales`.`sale`,
`g_sales`.`date`
FROM
`g_sales`
ORDER BY
`g_sales`.`date`
) AS `t` ORDER BY `t`.`date`;
results in
+------+------------+------+
| sale | date | test |
+------+------------+------+
| 106 | 2019-06-19 | 2703 |
| 85 | 2019-10-11 | 2685 |
| 81 | 2019-11-12 | 2681 |
| 96 | 2019-12-09 | 2695 |
| 104 | 2020-03-26 | 2701 |
| 87 | 2020-04-06 | 2687 |
| 94 | 2020-05-15 | 2693 |
| 107 | 2020-05-18 | 2704 |
| 98 | 2020-05-28 | 2697 |
| 103 | 2020-05-28 | 2700 |
| ... | .......... | .... |
+------+------------+------+
In MySQL test started at 1 and was incremented by one in each row. Adding a limit to the inner SELECT gets me a similar result in MariaDB.
SET @test := 0;
SELECT
*,
@test := @test + 1 AS `test`
FROM (
SELECT
`g_sales`.`sale`,
`g_sales`.`date`
FROM
`g_sales`
ORDER BY
`g_sales`.`date`
LIMIT 10 OFFSET 0
) AS `t`;
which results in
+------+------------+------+
| sale | date | test |
+------+------------+------+
| 106 | 2019-06-19 | 1 |
| 85 | 2019-10-11 | 2 |
| 81 | 2019-11-12 | 3 |
| 96 | 2019-12-09 | 4 |
| 104 | 2020-03-26 | 5 |
| 87 | 2020-04-06 | 6 |
| 94 | 2020-05-15 | 7 |
| 107 | 2020-05-18 | 8 |
| 98 | 2020-05-28 | 9 |
| 103 | 2020-05-28 | 10 |
+------+------------+------+
How can I get this result in MariaDB without adding a limit to the inner SELECT?
And why do I get this result when adding the LIMIT?