2013-07-09 84 views
1

sqlfiddle:http://sqlfiddle.com/#!2/9a8b3/1不同的結果sqlfiddle.com 30年5月5日和MariaDB的31年5月5日

以結構和數據查詢從小提琴, 導入到我的MariaDB的31年5月5日,我得到不同的結果:

sqlfiddle

PID NAME  LEAGUEPOINTS  TOTALLEAGUEPOINTS 
2 Peter 16,13,9,4,2   44 
1 Daniel 3425,543,234,43,29,22,21,21,19,17,13,12,12,12,11,9,9,9,8,7  4476 

MariaDB的

pid name leaguepoints  totalleaguepoints 
2 Peter 16,13,9,4,2    44 
1 Daniel 3425,543,234,43,29,22,21,21,19,17,13,12,12,12,11,9,9,9,8,7,7,6,5,5,4,4,4,3,3,2,1 4520 

查詢:

SELECT     
    p.pid, 
    p.name, 
    GROUP_CONCAT(gC.leaguepoints ORDER BY leaguepoints DESC) AS leaguepoints, 
    SUM(gC.leaguepoints) AS totalleaguepoints 
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 AND aSequence < 20 
GROUP BY p.pid 
ORDER BY p.name DESC 

任何想法爲什麼?

回答

1

怕我沒有MariaDB的手,但你可以嘗試下面正好看到用戶變量是如何輸出: -

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 

編輯 - 做了一些調查,看你的結果時,似乎MariaDB在子查詢中忽略了ORDER BY。因此,序列號是隨機的,並且在pid改變時也會重置(由於訂單沒有被修復,所以它是隨機的)。谷歌的一個位,似乎這是MariaDB的故意特徵。 SQL標準將一個表定義爲無序的一組行,並將一個子選擇視爲一個表,因此忽略順序 - https://kb.askmonty.org/en/why-is-order-by-in-a-from-subquery-ignored/

這是一個缺點。不知道現在有什麼辦法可以解決,因爲我現在無法想到。對於原來的問題,這是處理我認爲有必要使用相關的子選擇,這可能不會有效。

+0

http://pastebin.com/sF6qnMxR – DanFromGermany

+0

看着這些結果我認爲瑪麗亞可能忽略了內部子選擇中的ORDER BY子句。因此,序列號是隨機的順序,並且在pid更改時也會重置(由於順序不固定,所以它是隨機的)。 – Kickstart

+1

似乎這是MariaDB的故意功能。 SQL標準將一個表定義爲一組無序的行,而一個子選擇被視爲一個表,因此按順序被忽略 - https://kb.askmonty.org/en/why-is-order-by-in - 從子查詢 - 忽略/ – Kickstart

相關問題