MySQL在本地与远程服务器上的不同查询结果

时间:2015-03-13 11:05:17

标签: mysql environment-variables

我通过SQL查询将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列出现错误:

enter image description here

(请注意,在本地服务器上,记录无法编辑,删除,...而不是远程。请参阅前几列&w; wijzigen,verwijderen,...'只显示远程。不是我需要这样做,我只是想知道差异来自于什么。)

解释?如何解决或寻找导致此问题的原因?

1 个答案:

答案 0 :(得分: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 。您可以通过将变量操作组合到单个表达式中来解决此问题:

case

这实际上可以解决您的问题。顺便说一句,对于SQL代码,我通常更喜欢if()而不是case条件表达式,因为if()是标准的。但是,变量的使用对MySQL非常具体,{{1}}更容易输入。