Mysql选择并计算具有多个相同值的行

时间:2017-06-08 02:16:50

标签: mysql

我正在尝试创建一个允许我选择如下所示数据的查询

id  Counter_Type Champion_Name Counter_Lane 
---|------------|------------|--------------
1  | 1          | Ahri       | 1
2  | 1          | Ahri       | 2
5  | 1          | Ahri       | 2
3  | 1          | Ahri       | 3
4  | 1          | Ahri       | 2
6  | 1          | Teemo      | 1
7  | 1          | Warwick    | 4
8  | 1          | Warwick    | 4

它应该计算Counter_Lane,然后应该为该名称显示最多的Counter_Type,如下所示它应该如何显示数据:

id  Counter_Type Champion_Name Counter_Lane 
---|------------|------------|--------------
1  | 1          | Ahri       | 2
2  | 1          | Teemo      | 1
3  | 1          | Warwick    | 4

我已经尝试过以下代码,而且我最近的代码已经过了3个多小时了,所以有人可以帮忙。

SELECT 
    a.Counter_Type, Champion_For, a.Counter_Lane, a.Champion_Name, COUNT(*) as Amount, sum(vote_type = 'up') as Upvotes, sum(vote_type = 'down') as Downvotes, sum(vote_type = 'up')-sum(vote_type = 'down') as Totalvotes 
FROM Champion_Counters_Data a 
JOIN ( SELECT c.Counter_Lane, c.Champion_Name, COUNT(*) magnitude 
FROM Champion_Counters_Data c 
WHERE 
    Champion_For = "Aatrox" AND Counter_Type = 1 GROUP BY Champion_Name, Counter_Lane ORDER BY magnitude) b ON a.Champion_Name = b.Champion_Name AND b.Counter_Lane = a.Counter_Lane 
GROUP BY Champion_Name

3 个答案:

答案 0 :(得分:0)

label:nth-child(4n+2) {
  width: 12px;
  position: relative;    
  display: block;
  margin-right: 3px;
  overflow: hidden;
}

label:nth-child(4n+4)::before {
  content: '\f006';
  display: block;
  width: 11px;    
  overflow: hidden;
} 

此查询基于您所提供的表格所需的输出。

答案 1 :(得分:0)

尝试以下SQL语句:

SELECT Counter_Type , Champion_Name, MAX(Counter_Lane) 'Counter_Lane'
FROM(
    SELECT Counter_Type , Champion_Name , Counter_Lane
    FROM Champion_Counters_Data 
    GROUP BY Counter_Type , Champion_Name 
    HAVING COUNT(Counter_Lane) >= 2
) tb
GROUP BY Counter_Type , Champion_Name

答案 2 :(得分:0)

由于MySQL不允许使用Windows函数或CTE,因此需要许多子查询。

Rextester Demo

SELECT @rn := @rn + 1 as id,
t3.* 
from 
    (select t1.*
    FROM
      (SELECT counter_type,
              champion_name,
              counter_lane,
              count(*) AS cnt
       FROM table53
       GROUP BY champion_name,
                counter_lane
      ) t1
    INNER JOIN
      (SELECT counter_type,
              champion_name,
              max(cnt) AS mcnt
       FROM
            (SELECT counter_type,
                 champion_name,
                 counter_lane,
                 count(*) AS cnt
          FROM table53
          GROUP BY champion_name,
                   counter_lane
            ) t
       GROUP BY counter_type,
                champion_name
       ) t2 
    ON t1.counter_type=t2.counter_type
    AND t1.champion_name=t2.champion_name
    AND t1.cnt=t2.mcnt
    ) t3
,(SELECT @rn := 0) t
;

输出

+-----+--------------+---------------+--------------+-----+
|  id | counter_type | champion_name | counter_lane | cnt |
+-----+--------------+---------------+--------------+-----+
|   1 |            1 | Ahri          |            2 |   3 |
|   2 |            1 | Teemo         |            1 |   1 |
|   3 |            1 | Warwick       |            4 |   2 |
+-----+--------------+---------------+--------------+-----+

想法是先按champion_namecounter_lane进行分组,然后获取count。因此,对于Ahri,您将cnt作为3。现在使用另一个子查询来获取相应的counter_lane2Ahri。最后,使用序列号生成ID为123等。