我有两张桌子。这是关于国际象棋比赛和它的球员。
在第一张桌子中,我有关于玩家的详细信息。 在第二个表格中,我有关于游戏的详细信息(玩家1,玩家2),(游戏类型)。
两个表链接在一起。(两个参考,玩家1与第一个表的id链接,玩家2再次与第一个表的id链接)
我的任务是进行审讯。
我必须显示,玩过大多数游戏的玩家。
问题在于我将这个播放器放在两个表中。 到目前为止,我设法显示了一个玩家在一个列中玩的游戏的最大数量:
select Player1,count(Player1) as number_ofgames
group by Player1
having count(Player 1) in
(select max(count(Player 1)) from games group by Player1);
答案 0 :(得分:1)
我不确定您的表格结构因此假设您的player
表格包含列player_id
以及game
表格player1_id
和player2_id
此查询可能符合您对Oracle 12c的要求。
select
p.player_id,
count(*) as games_played
from
player p join game g on p.player_id = g.player1_id or p.player_id = g.player2_id
group by
p.player_id
order by
2 desc
fetch first 1 rows with ties;
如果多行与第N行的值匹配,则使用WITH TIES
子句可能会导致返回更多行。
有关详细信息,请参阅http://www.oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1.php#top-n。
在MySQL中使用LIMIT
代替:
select
p.player_id,
count(*) as games_played
from
player p join game g on p.player_id = g.player1_id or p.player_id = g.player2_id
group by
p.player_id
order by
2 desc
limit 1;
在此处阅读更多内容:http://dev.mysql.com/doc/refman/5.0/en/select.html。
答案 1 :(得分:0)
如果您使用的是11G R2,则可以使用listagg功能。 如果没有,很容易找到不同的方式
With distrib as
(select O as dabegin, 17 as daend, 'under 18' as category from dual
union all
select 18 as dabegin ,25 as daend, 'between 18-25' as category from dual
union all
select 26 as dabegin ,30 as daend, 'between 26-30' as category from dual
)
, games as
(select
p.player_id,
count(*) as games_played
from
player p join game g on p.player_id = g.player1_id or p.player_id = g.player2_id
group by
p.player_id)
select max(games_played), listagg(player_id,',') WITHIN GROUP (ORDER BY player_id) as 'winner(s)', category
from distrib d, games g, players p
where p.player_id=g.player_id
and g. age between d.dabegin and daend
group by g.category