Different results in sqlfiddle.com 5.5.30 and MariaDB 5.5.31

后端 未结 1 404
-上瘾入骨i
-上瘾入骨i 2021-01-25 23:56

sqlfiddle: http://sqlfiddle.com/#!2/9a8b3/1

Taking the structure and data and query from the fiddle, importing into my MariaDB 5.5.31, I get different results:

s

相关标签:
1条回答
  • 2021-01-26 00:43

    Afraid I don't have MariaDB to hand, but could you try the following just to see how the user variables are output:-

    SELECT  *
    FROM test_golf_player p
    LEFT JOIN 
    (
        SELECT pid, leaguepoints, @Sequence:=IF(@PrevPid = pid, @Sequence + 1, 0) AS aSequence, @PrevPid := pid
        FROM
        (
            SELECT pid, leaguepoints
            FROM test_golf_card 
            ORDER BY pid, leaguepoints DESC
        ) Sub1
        CROSS JOIN (SELECT @PrevPid := 0, @Sequence := 0) Sub2
    ) gC
    ON p.pid = gC.pid 
    ORDER BY p.name DESC 
    

    EDIT - Doing a bit of investigation looking at your results it seems that MariaDB has ignored the ORDER BY in the sub query. Hence the sequence number is in a random order, and also resets when the pid changes (which it does randomly due to the order not being fixed). Bit of a google and it seems this is a deliberate feature of MariaDB. The SQL standard defines a table as an unordered set of rows, and a sub select is treated as a table hence the order by is ignored - https://kb.askmonty.org/en/why-is-order-by-in-a-from-subquery-ignored/ .

    It is a bit of a disadvantage. Not sure there is a work around as I can't think of one at the moment. For the original problem that this was to deal with I think it would be necessary to use correlated sub selects which would probably not be efficient.

    0 讨论(0)
提交回复
热议问题