I'm not sure about your table structures therefore assuming your player
table has column player_id
as well as game
table player1_id
and player2_id
this query may met your requirements on 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;
Using the WITH TIES
clause may result in more rows being returned if multiple rows match the value of the Nth row.
For more details read http://www.oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1.php#top-n.
In MySQL use LIMIT
instead:
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;
Read more here: http://dev.mysql.com/doc/refman/5.0/en/select.html.