简体   繁体   中英

Select Most Frequent from Multiple Columns

I've got a table like;

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

etc..

With various values for Player, and Warcaster. WinningCaster / LosingCaster is a finite namelist, and I want to make a query that will find me which name occurs the most often, across both columns, both with and without a particular player entry. IE Player A should return WarcasterA with 2, and an overall Query should return WarcasterA with 3.

So far I've only been able to get the most frequent from either column, not from both, with the following;

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

Use 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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