在锦标赛中查找排名 Table
Finding Standings Table in a tournament
我有 2 table
create table players
(name text,
id serial primary key);
create table matches
(winner integer references players(id),
loser integer references players(id),
id serial primary key);
我必须制作一个名为 "standings" 的 table,其中包含:
player_id,player_name,total_wins,total_matches
如何进行?
像这样:
select p.id, p.name,
count(w.id) as total_wins,
count(l.id) + count(w.id) as total_matches
from players p
left join matches w on w.winner = p.id
left join matches l on l.loser = p.id
group by p.id, p.name;
我有 2 table
create table players
(name text,
id serial primary key);
create table matches
(winner integer references players(id),
loser integer references players(id),
id serial primary key);
我必须制作一个名为 "standings" 的 table,其中包含:
player_id,player_name,total_wins,total_matches
如何进行?
像这样:
select p.id, p.name,
count(w.id) as total_wins,
count(l.id) + count(w.id) as total_matches
from players p
left join matches w on w.winner = p.id
left join matches l on l.loser = p.id
group by p.id, p.name;