从多列中选择最常用

时间:2016-10-25 17:43:06

标签: mysql sql

我有一张像这样的桌子;

ID | Winner   | Loser    | WinningCaster | LosingCaster
0  | Player A | Player B | Warcaster A   | Warcaster B
1  | Player A | Player B | Warcaster C   | Warcaster A
2  | Player C | Player D | Warcaster A   | Warcaster B

等。

使用Player和Warcaster的各种值。 WinningCaster / LosingCaster是一个有限的名单,我想创建一个查询,它会找到最常出现在两个列上的名称,包括和不包含特定的玩家条目。 IE Player A应该返回带有2的WarcasterA,并且整个Query应该返回带有3的WarcasterA。

到目前为止,我只能从两个列中获得最频繁的,而不是从两个列,以下;

SELECT 
    ID, Winner, Loser, CasterWinner, Count(CasterWinner) AS Occ
FROM 
    `Games`
GROUP BY 
    CasterWinner
ORDER BY 
    Occ DESC
LIMIT 1

1 个答案:

答案 0 :(得分:0)

使用union all

select caster, count(*)
from ((select casterwinner as caster from games
      ) union all
      (select casterloser from games
      )
     ) c
group by caster
order by count(*) desc
limit 1;