MySQL查询计数和分数多列

时间:2013-11-16 04:19:49

标签: mysql sql

我有一个类似于以下内容的MySQL表:

player_rankings | CREATE TABLE `player_rankings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `award_id` int(11) NOT NULL,
  `rank_1` int(11) DEFAULT NULL,
  `rank_2` int(11) DEFAULT NULL,
  `rank_3` int(11) DEFAULT NULL,
  `rank_4` int(11) DEFAULT NULL,
  `rank_5` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`award_id`)
) 

rank_1rank_5包含我在不同表格中的玩家ID,但出于此查询的目的,它不相关。我要做的是计算每个rank_*列中显示的值的次数,然后给它以下数量的点:

  • rank_1 - 5分
  • rank_2 - 4分
  • rank_3 - 3分
  • rank_4 - 2分
  • rank_1 - 1分

最终结果将根据玩家ID的总点数从最高到最低(降序)排序。

3 个答案:

答案 0 :(得分:1)

获得每个等级的积分总和:

SELECT user_id,
  sum(rank1Points) rank1Points,
  sum(rank2Points) rank2Points,
  sum(rank3Points) rank3Points,
  sum(rank4Points) rank4Points,
  sum(rank5Points) rank5Points
FROM (
  SELECT pr.rank_1 user_id, count(*) * 5 rank1Points, 0 rank2Points, 0 rank3Points, 0 rank4Points, 0 rank5Points
  FROM player_rankings pr
  GROUP BY pr.rank_1
  UNION ALL
  SELECT pr.rank_2, 0, count(*) * 4, 0, 0, 0
  FROM player_rankings pr
  GROUP BY pr.rank_2
  UNION ALL
  SELECT pr.rank_3, 0, 0, count(*) * 3, 0, 0
  FROM player_rankings pr
  GROUP BY pr.rank_3
  UNION ALL
  SELECT pr.rank_4, 0, 0, 0, count(*) * 2, 0
  FROM player_rankings pr
  GROUP BY pr.rank_4
  UNION ALL
  SELECT pr.rank_5, 0, 0, 0, 0, count(*)
  FROM player_rankings pr
  GROUP BY pr.rank_5
) s
GROUP BY user_id

小提琴here

要在一列中获得总点数:

SELECT user_id,
  sum(rankPoints) rankPoints
FROM (
  SELECT pr.rank_1 user_id, count(*) * 5 rankPoints
  FROM player_rankings pr
  GROUP BY pr.rank_1
  UNION ALL
  SELECT pr.rank_2, count(*) * 4
  FROM player_rankings pr
  GROUP BY pr.rank_2
  UNION ALL
  SELECT pr.rank_3, count(*) * 3
  FROM player_rankings pr
  GROUP BY pr.rank_3
  UNION ALL
  SELECT pr.rank_4, count(*) * 2
  FROM player_rankings pr
  GROUP BY pr.rank_4
  UNION ALL
  SELECT pr.rank_5, count(*)
  FROM player_rankings pr
  GROUP BY pr.rank_5
) s
GROUP BY user_id

小提琴here

答案 1 :(得分:0)

对于你的表结构可能是好的解决方案是在脚本中计算所有。只需获取所有行然后计算。

如果您想在一个SQL查询中进行all all,那么您可以尝试使用UNION for 5子查询

SELECT user_id, rank FROM(
   SELECT rank_1 AS user_id, 5 AS rank FROM player_rankings
   UNION
   SELECT rank_2 AS user_id, 4 AS rank FROM player_rankings
   ....
) AS u;

没有工作脚本,但我希望您理解我的解决方案

答案 2 :(得分:0)

尝试

SELECT user_id, SUM(6 - rank) points
  FROM
(
  SELECT rank,
         CASE rank 
            WHEN 1 THEN rank_1
            WHEN 2 THEN rank_2
            WHEN 3 THEN rank_3
            WHEN 4 THEN rank_4
            WHEN 5 THEN rank_5
         END user_id
    FROM player_rankings t CROSS JOIN
  (
    SELECT 1 rank UNION ALL
    SELECT 2 UNION ALL
    SELECT 3 UNION ALL
    SELECT 4 UNION ALL
    SELECT 5 
  ) i
) q
 GROUP BY user_id
 ORDER BY points DESC

此查询背后的想法是首先取消忽略您的表,然后在分组时计算每个user_id的点数,然后按点对结果集进行排序。一站式查询。

示例输出:

| USER_ID | POINTS |
|---------|--------|
|      50 |     13 |
|      10 |     12 |
|      30 |     12 |
|      20 |      9 |
|      40 |      5 |
|      60 |      4 |
|      70 |      3 |
|      80 |      2 |

这是 SQLFiddle 演示