2015-03-13 29 views
0

我通過SQL查詢將MySQL數據庫的副本發送到遠程服務器。MySQL在本地和遠程服務器上的不同查詢結果

遠程服務器:

Variable_name    Value 
protocol_version   10 
slave_type_conversions  
version      10.0.11-MariaDB 
version_comment    MariaDB Server 
version_compile_machine  x86_64 
version_compile_os   Linux 
version_malloc_library  system 

本地服務器:

Variable_name    Value 
innodb_version    5.6.21 
protocol_version   10 
slave_type_conversions  
version      5.6.21 
version_comment    MySQL Community Server (GPL)  
version_compile_machine  x86 
version_compile_os   Win32 

當我運行下面的查詢,這產生兩種服務器上不同的結果。

select K, S, rn as R, G, V, T, Naam, Club, R1, R2, R3, R4, R5, R6, Totaal 
from (
    select K, S, G, V, T, Naam, Club, R1, R2, R3, R4, R5, R6, R1+R2+R3+R4+R5+R6 as Totaal, 
     @rn := case when @prev = S 
        then @rn + 1 
        else 1 
       end as rn, 
     @prev := S 
    from (
     select K, substr(K,1,1) as S, G, V, T, L.Naam, C.naam as Club, coalesce(R1,0) R1, coalesce(R2,0) R2, coalesce(R3,0) R3, coalesce(R4,0) R4, coalesce(R5,0) R5, coalesce(R6,0) R6 
     from (
      select clubid, naam from club) as C 
       join (select clubid, bondsnummer, if(mv="M", "Dhr.", "Mw.") as G, voorletters as V, tussenvoegsel as T, achternaam as Naam from lid where mv="M") as L on C.clubid=L.clubid 
       join (select bondsnummer, klasse as K, compdnid from deelnemer) as D on L.bondsnummer=D.bondsnummer 
       left join (select compdnid, rondeid, sum(score) as R1 from score where rondeid=1 group by compdnid,rondeid) as R1 on D.compdnid=R1.compdnid 
       left join (select compdnid, sum(score) as R2 from score where rondeid=2 group by compdnid,rondeid) as R2 on R1.compdnid=R2.compdnid 
       left join (select compdnid, sum(score) as R3 from score where rondeid=3 group by compdnid,rondeid) as R3 on R2.compdnid=R3.compdnid 
       left join (select compdnid, sum(score) as R4 from score where rondeid=4 group by compdnid,rondeid) as R4 on R3.compdnid=R4.compdnid 
       left join (select compdnid, sum(score) as R5 from score where rondeid=5 group by compdnid,rondeid) as R5 on R4.compdnid=R5.compdnid 
       left join (select compdnid, sum(score) as R6 from score where rondeid=6 group by compdnid,rondeid) as R6 on R5.compdnid=R6.compdnid 
    ) as S, (select @prev := 0, @rn := 0) as vars 
    order by S, Totaal desc 
) as X 
where Totaal > 0 
limit 0,500 

結果本地服務器上,與@Rn的正確編號爲R:

enter image description here

而且在R列遠程服務器numering出錯:

enter image description here

(請注意,在本地服務器上,記錄不能被編輯,刪除,......而不是遠程。查看前幾列'wij zigen,verwijderen,...',只是顯得很遙遠。不是我需要這樣做,我只是想知道有什麼區別。)

解釋?如何解決或尋找是什麼原因造成的?

+0

您正在使用'limit'而沒有'order by'(在外部查詢中)。結果集的排序不受子查詢中的「order by」保證,特別是在使用諸如MariaDB之類的並行數據庫時。如果需要一致的結果,請在外部查詢中輸入'order by'。 – 2015-03-13 11:06:59

+0

我現在試過了。結果是,我在兩個服務器上獲得與在遠程服務器上運行(仍未更改)查詢時相同的錯誤模式。所以這絕對是問題,但你的建議似乎不是確切的或完整的解決方案?或者兩個服務器的查詢應該不同? – user1837293 2015-03-13 11:37:55

回答

0

爲什麼要使用子查詢?只要把所有的邏輯在一個查詢:

select K, S, G, V, T, Naam, Club, R1, R2, R3, R4, R5, R6, 
     (R1+R2+R3+R4+R5+R6) as Totaal, 
    @rn := case when @prev = S 
       then @rn + 1 
       else 1 
      end as rn, 
    @prev := S 
from (
    select K, substr(K,1,1) as S, G, V, T, L.Naam, C.naam as Club, coalesce(R1,0) R1, coalesce(R2,0) R2, coalesce(R3,0) R3, coalesce(R4,0) R4, coalesce(R5,0) R5, coalesce(R6,0) R6 
    from (
     select clubid, naam from club) as C 
      join (select clubid, bondsnummer, if(mv="M", "Dhr.", "Mw.") as G, voorletters as V, tussenvoegsel as T, achternaam as Naam from lid where mv="M") as L on C.clubid=L.clubid 
      join (select bondsnummer, klasse as K, compdnid from deelnemer) as D on L.bondsnummer=D.bondsnummer 
      left join (select compdnid, rondeid, sum(score) as R1 from score where rondeid=1 group by compdnid,rondeid) as R1 on D.compdnid=R1.compdnid 
      left join (select compdnid, sum(score) as R2 from score where rondeid=2 group by compdnid,rondeid) as R2 on R1.compdnid=R2.compdnid 
      left join (select compdnid, sum(score) as R3 from score where rondeid=3 group by compdnid,rondeid) as R3 on R2.compdnid=R3.compdnid 
      left join (select compdnid, sum(score) as R4 from score where rondeid=4 group by compdnid,rondeid) as R4 on R3.compdnid=R4.compdnid 
      left join (select compdnid, sum(score) as R5 from score where rondeid=5 group by compdnid,rondeid) as R5 on R4.compdnid=R5.compdnid 
      left join (select compdnid, sum(score) as R6 from score where rondeid=6 group by compdnid,rondeid) as R6 on R5.compdnid=R6.compdnid 
) as S, (select @prev := 0, @rn := 0) as vars 
where (R1+R2+R3+R4+R5+R6) > 0 
order by S, Totaal desc 
limit 0, 500; 

編輯:

您的問題可能沒有實際是limitorder by。這可能是變數。 MySQL確實而不是保證select子句中表達式的求值順序,因此可以在使用prev之前設置它。您可以通過將可變操作組合成單個表達式來解決此問題:

(@rn := if(@prev = S, @rn + 1, 
       if(@prev := S, 1, 1) 
      ) 
    ) as rn 

這實際上可能會解決您的問題。順便說一下,對於SQL代碼,我通常更喜歡case而不是if()作爲條件表達式,因爲case是標準的。但是,變量的使用非常特定於MySQL,並且if()更易於輸入。

+0

那實際上只是爲了防止@prev出現在最終的queryresult中。如果這是唯一的解決方案,我可能會放棄它。其他方式來實現呢? – user1837293 2015-03-13 11:59:53

+0

@ user1837293。 。 。看我的編輯。變量賦值實際上可能是問題的根源。 – 2015-03-13 12:22:02

+0

我現在就試過了。但是它不會做出改變:在本地服務器上的結果是正常的,在遠程服務器上如前所述錯誤。 – user1837293 2015-03-13 16:15:04

相關問題