Mysql 将来自不同列和计数的值分组
我有这张桌子:Mysql 将来自不同列和计数的值分组,mysql,sql,count,group-by,Mysql,Sql,Count,Group By,我有这张桌子: UNIQUE_ID | WINNER_ID | FINALIST_ID ________1 | ________1 | __________2 ________2 | ________1 | __________3 ________3 | ________3 | __________1 ________4 | ________1 | __________2 我需要一份所有选手(优胜者和决赛选手)的名单,以及他们获得第一名或第二名的次数 在这种情况下,它将是: PLAYER_ID
UNIQUE_ID | WINNER_ID | FINALIST_ID
________1 | ________1 | __________2
________2 | ________1 | __________3
________3 | ________3 | __________1
________4 | ________1 | __________2
我需要一份所有选手(优胜者和决赛选手)的名单,以及他们获得第一名或第二名的次数
在这种情况下,它将是:
PLAYER_ID | WINNER_TIMES | FINALIST_TIMES
________1 | ___________3 | _____________1
________2 | ___________0 | _____________2
________3 | ___________1 | _____________1
这里已经有人问过类似的问题(),但我不明白答案
select coalesce(winner_id, finalist_id) as PLAYER_ID
, count(winner_id) as WINNER_TIMES
, count(finalist_id) as FINALIST_TIMES
from (
select winner_id
, null as finalist_id
from YourTable
union all
select null
, finalist_id
from YourTable
) as SubQueryAlias
group by
coalesce(winner_id, finalist_id)
试试这个:
Select
user_id as user,
winner_temp.count(1) as winning_count
finalist_temp.count(1) as runner_up_count
from
user_table
left join
(Select winner_id, count(1) from table group by winner_id) as winner_temp on (user_table.user_id = winner_temp.winner_id)
left join
(Select finalist_id, count(1) from table group by finalist_id) as finalist_temp on
(user_table.user_id = finalist_temp.finalist_id)
谢谢你,安多玛。我仍然需要学习更多关于多重选择的知识。