MYSQL绑定处理没有返回正确的结果

时间:2013-10-09 17:44:23

标签: mysql sum tie

我正在使用MYSQL并尝试使用MYSQL的能力进行排名。

我的查询是:

    SELECT petz.s_name,
       petz.breed,
       a.num,
       sum(a.rank) AS rank
FROM wins_conf a
JOIN
  (SELECT DISTINCT rank
   FROM wins_conf
   ORDER BY rank DESC LIMIT 10) b ON a.rank = b.rank
JOIN petz ON a.num=petz.num
GROUP BY petz.num
ORDER BY petz.breed,
         rank DESC

返回结果:

                                                                  sum(Rank)
INSANITY'S ACE OF SPADES           Collie          1026           58
INSANITY'S SAVE ME                 Collie          1000           31
STAR GAZER'S BEAUTIFUL LIES        Collie          1039           24
BANYON'S ALL IS FORGIVEN           Collie          1009           19
FELIXTOWE CHERRY BLOSSOM           Collie          1214           18
KE'S PRICELESS FIGUREINE           Collie          1004           13
NOVABLUE'S LOVES UNENDING LEGACY   Collie          1211           12
STAR GAZER'S WARRIOR OF MY HEART   Collie          1059            9
INSANITY'S BE MINE                 Collie          1028            9
STAR GAZER'S A WILDCAT'S REVENGE   Collie          1040            5
KE'S TRICKS OF THE TRADE           Collie          1005            5

记录1059(STAR GAZER'S OF HEAVARD OF THE HEART)返回9作为等级,但它应该是12,基于数据库中的记录总和()

                                                       Rank
conf    33    13    1059    Best of Breed    0    0    5    0   2
conf    78    3139  1059    Best of Breed    0    0    4    0   2
conf    82    2518  1059    Best of Breed    0    0    1    0   2
conf    81    13    1059    Best in Specialty0    0    1    0   2
conf    79    13    1059    Best of Breed    0    0    1    0   2

通过一些调查我发现它只会看到排名列中sum()的最后3条记录,如果1大于或等于4

有关如何纠正此问题的任何建议吗?


编辑/更新回复AgRizzo 我刚刚删除了全名和品种以便于阅读,这就是我想要的,排名明智。我想显示排名,重复但只有10(包括他们的重复)。

     num          rank
1    1026         58
2    1000         31
3    1039         24
4    1009         19
4    1214         19
5    1004         13
6    1211         12
6    1059         12
7    1028          9
8    1005          5
8    1040          5
9    1010          3
10    1276          1

我在这里设置了一些基本数据:http://sqlfiddle.com/#!2/7e2992 如上所述,它缺少一些绒毛内容,但在排名中不需要该内容。

3 个答案:

答案 0 :(得分:0)

试试这个

   select petz.s_name, petz.breed, a.num, sum(a.rank) as rank
   from wins_conf a
   JOIN petz ON a.num=petz.num 
   GROUP BY petz.num 
   ORDER BY petz.breed, rank DESC LIMIT 10

答案 1 :(得分:0)

以下是排名

的变体
SELECT s_name
    , breed
    , num
    , @denserank := IF(@prevrank = rank, @denserank, @denserank + 1 ) as DenseRank
    , @prevrank := rank AS rank
FROM (
  SELECT petz.s_name AS s_name
      , petz.breed AS breed
      , a.num AS num
      , sum(a.rank) as rank
  FROM wins_conf a
  JOIN petz 
    ON a.num=petz.num
  WHERE petz.breed = 'Collie' 
  GROUP BY petz.s_name, petz.breed, a.num
  ORDER BY petz.breed, rank DESC) AS temp1
JOIN (SELECT @prevscore := NULL, @denserank := 0) AS dummy
WHERE @denserank < 5

这是在SQLFiddle http://sqlfiddle.com/#!2/7e2992/7上。由于您的数据有限,此示例列出了前5个,否则将选择所有记录。将WHERE子句更改为列出站点的前10名

答案 2 :(得分:0)

在其他RDBMS中,您可能会使用CTE来计算每个petz&#39;总得分(您的SUM(rank))然后是DENSE_RANK函数,根据这些得分对它们进行排名。

由于MySQL缺乏这些便利,我们可以使用VIEW或子查询而不是CTE。 DENSE_RANK可以用会话变量计算,如@AgRizzo's answer,或简单地一(1)加上不同分数的计数&#34;更好&#34;而不是一个特定的分数。

我要用VIEW而不是子查询来组合这一切,因为我认为这会使查询的逻辑显而易见:

SET SESSION sql_mode='ANSI';

-- Compute each petz' score
CREATE OR REPLACE VIEW scorez AS
  SELECT "num", SUM("rank") AS "score"
    FROM wins_conf
GROUP BY 1;

-- Compute each scored petz' DENSE_RANK
CREATE OR REPLACE VIEW standingz AS
   SELECT my."num", 
          my."score",
          COUNT(DISTINCT their."score") + 1 AS "rank" -- DENSE_RANK
     FROM scores my
LEFT JOIN scores their
          ON their.score > my.score
 GROUP BY 1, 2;

-- Now fetch the full result set
    SELECT standingz.rank, petz.*
      FROM petz
INNER JOIN standingz
           ON petz.num = standingz.num
  ORDER BY standingz.rank ASC;