简体   繁体   English

mysql-为什么这两个查询返回不同的结果

[英]mysql- Why do these two queries return different results

For some reason these 2 mysql queries, which I thought were essentially the same thing with different formatting, are returning different results for the sum value, and I'm not entirely sure why. 由于某些原因,我认为这2个mysql查询本质上是相同的东西,但格式不同,它们返回的总和是不同的结果,我不确定为什么。

query 1(returning correct value) 查询1(返回正确值)

SELECT sum((getRankint(cs_times.mapid, 1123, 0, 0))) AS rankval
FROM cs_times
INNER JOIN cs_maps ON cs_times.mapid = cs_maps.mapid
WHERE stage =0
AND TYPE =0
AND active =1
AND playerid =1123
AND getRankint(
cs_times.mapid, 1123, 0, 0
) <11

query 2 查询2

SELECT @rankval := getRankint(cs_times.mapid, 1123, 0, 0) AS rankval, 
sum(@rankval)
FROM cs_times
INNER JOIN cs_maps ON cs_times.mapid = cs_maps.mapid
WHERE stage =0
AND TYPE =0
AND active =1
AND playerid =1123
AND getRankint(
cs_times.mapid, 1123, 0, 0
) < 11

If I compare the tables side by side without the sum function, they come out exactly the same. 如果我不使用sum函数并排比较表,它们的输出结果完全相同。 However the total sum value is coming out different. 但是总和值是不同的。 Any idea what could be causing this? 知道是什么原因造成的吗? Thanks. 谢谢。

(Also there's a redundant function call in the where section of the query, is there a way to clear that out so it's not calling the function twice?) (在查询的where部分中还有一个冗余的函数调用,是否有一种方法可以清除该错误,以便它不会两次调用该函数?)

If I compare the tables side by side without the sum function, they come out exactly the same. 如果我不使用sum函数并排比较表,它们的输出结果完全相同。 However the total sum value is coming out different. 但是总和值是不同的。 Any idea what could be causing this? 知道是什么原因造成的吗?

It is because of combining the aggregate with row level column value. 这是因为将汇总与行级列值结合在一起。

There will be only one row result from your query. 您的查询将只有一行结果。

  • First value represents result of getRankint on first row column value fetched and 第一个值表示获取的第一行列值的getRankint结果,并且
  • the second represents sum of such ranks = count(*) * getRankit . 第二个代表此类等级的总和= count(*) * getRankit Here the count value is the number of rows fetched that satisfies the where condition. 这里的计数值是满足where条件的已获取的行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM