CREATE TABLE matches (
match_id BIGSERIAL PRIMARY KEY,
tournamentid INTEGER,
player1_id INTEGER,
player2_id INTEGER CHECK (player1_id < player2_id),
result INTEGER CHECK (result IN (0, 1, 2)),
FOREIGN KEY(tournamentid, player1_id) REFERENCES enroll(tournament, player_id),
FOREIGN KEY(tournamentid, player2_id) REFERENCES enroll(tournament, player_id),
UNIQUE(tournamentid, player1_id, player2_id)
);
CREATE VIEW player_standings AS (
SELECT tournaments.tournament_id,
tournaments.tournament_name,
enroll.player_id,
players.name,
CASE
WHEN matches.result = 1 THEN COUNT(matches.player1_id)
WHEN matches.result = 2 THEN COUNT(matches.player2_id)
END AS wins,
COUNT(enroll.player_id IN (matches.player1_id, matches.player2_id)) AS match_played
FROM players
INNER JOIN enroll ON enroll.player_id = players.id
INNER JOIN tournaments ON tournaments.tournament_id = enroll.tournament
LEFT JOIN matches ON (matches.player1_id = enroll.player_id) or
(matches.player2_id = enroll.player_id)
GROUP BY tournaments.tournament_id, tournaments.tournament_name,
enroll.player_id, players.name, matches.result
ORDER BY tournaments.tournament_id, wins DESC
);
我似乎不能正确地计算出获胜的比赛次数。我认为这个问题与matches表的左连接有关。
其思想是从matches表的
result
列中读取结果,其中的结果解释为:0 = draw
1 = player1 won
2 = player2 won
使用当前的模式,我可以正确地获得每个玩家的比赛次数,但不能得到比赛次数。
如果可能的话,我还希望添加绑定的匹配数,而不必拆分成多个视图或表。有什么建议吗?
最佳答案
因为你没有包括你所有的模式,我做了一些有根据的猜测,并提出了这个观点,我认为应该工作。我包括了一些额外的损失计数和抽签,我发现更容易验证结果与所有的结果在视图中出现。
CREATE VIEW player_standings AS (
SELECT
tournaments.tournament_id as t_id
, tournaments.tournament_name
, enroll.player_id
, players.name
, COUNT(
CASE
WHEN enroll.player_id = matches.player1_id AND matches.result = 1 THEN 1
WHEN enroll.player_id = matches.player2_id AND matches.result = 2 THEN 1
END) AS wins
, COUNT(
CASE
WHEN enroll.player_id = matches.player1_id AND matches.result = 2 THEN 1
WHEN enroll.player_id = matches.player2_id AND matches.result = 1 THEN 1
END) AS losses
, COUNT(CASE WHEN matches.result = 0 THEN 1 END) AS draws
, COUNT(match_id) AS matches_played
FROM players
INNER JOIN enroll ON enroll.player_id = players.id
INNER JOIN tournaments ON tournaments.tournament_id = enroll.tournament
LEFT JOIN matches ON matches.tournamentid = tournaments.tournament_id
AND enroll.player_id IN (matches.player1_id, matches.player2_id)
GROUP BY
tournaments.tournament_id,
tournaments.tournament_name,
enroll.player_id,
players.name
ORDER BY
tournaments.tournament_id,
wins DESC,
matches_played DESC
);
下面是我创建的SQL Fiddle的降价输出:
SQL Fiddle
PostgreSQL 9.3架构设置:
create table players (
id int primary key,
name varchar(20)
);
insert into players values
(1, 'Player 1'),(2, 'Player 2'),
(3, 'Player 3'),(4, 'Player 4'),(5, 'Player 5');
create table tournaments (
tournament_id int primary key,
tournament_name varchar(20)
);
insert into tournaments values (1, 'Tournament 1'),(2, 'Tournament 2');
create table enroll (
tournament int,
player_id int,
primary key (tournament, player_id),
foreign key (tournament) references tournaments(tournament_id),
foreign key (player_id) references players(id)
);
insert into enroll values
(1,1),(1,2),(1,3),(1,4),(1,5),
(2,1),(2,2),(2,3),(2,4),(2,5);
CREATE TABLE matches (
match_id bigserial PRIMARY KEY,
tournamentid INTEGER,
player1_id INTEGER,
player2_id INTEGER CHECK (player1_id < player2_id),
result INTEGER CHECK (result IN (0, 1, 2)),
FOREIGN KEY(tournamentid, player1_id) REFERENCES enroll(tournament, player_id),
FOREIGN KEY(tournamentid, player2_id) REFERENCES enroll(tournament, player_id),
UNIQUE(tournamentid, player1_id, player2_id)
);
insert into matches (tournamentid, player1_id, player2_id, result) values
(1, 1, 2, 1) -- 1 win 2 loss
,(1, 1, 3, 1) -- 1 win 3 loss
,(1, 2, 3, 2) -- 2 win 2 loss
,(1, 1, 5, 1) -- 1 win 5 loss
,(2, 2, 4, 0) -- 2 draw 4 draw
,(2, 1, 2, 1) -- 1 win 2 loss
,(2, 3, 4, 2) -- 4 win 3 loss
;
CREATE VIEW player_standings AS (
SELECT
tournaments.tournament_id as t_id
, tournaments.tournament_name
, enroll.player_id
, players.name
, COUNT(
CASE
WHEN enroll.player_id = matches.player1_id AND matches.result = 1 THEN 1
WHEN enroll.player_id = matches.player2_id AND matches.result = 2 THEN 1
END) AS wins
, COUNT(
CASE
WHEN enroll.player_id = matches.player1_id AND matches.result = 2 THEN 1
WHEN enroll.player_id = matches.player2_id AND matches.result = 1 THEN 1
END) AS losses
, COUNT(CASE WHEN matches.result = 0 THEN 1 END) AS draws
, COUNT(match_id) AS matches_played
FROM players
INNER JOIN enroll ON enroll.player_id = players.id
INNER JOIN tournaments ON tournaments.tournament_id = enroll.tournament
LEFT JOIN matches ON matches.tournamentid = tournaments.tournament_id
AND enroll.player_id IN (matches.player1_id, matches.player2_id)
GROUP BY
tournaments.tournament_id,
tournaments.tournament_name,
enroll.player_id,
players.name
ORDER BY
tournaments.tournament_id,
wins DESC,
matches_played DESC
);
问题1:
select * from player_standings
Results:
| t_id | tournament_name | player_id | name | wins | losses | draws | matches_played |
|------|-----------------|-----------|----------|------|--------|-------|----------------|
| 1 | Tournament 1 | 1 | Player 1 | 3 | 0 | 0 | 3 |
| 1 | Tournament 1 | 3 | Player 3 | 1 | 1 | 0 | 2 |
| 1 | Tournament 1 | 2 | Player 2 | 0 | 2 | 0 | 2 |
| 1 | Tournament 1 | 5 | Player 5 | 0 | 1 | 0 | 1 |
| 1 | Tournament 1 | 4 | Player 4 | 0 | 0 | 0 | 0 |
| 2 | Tournament 2 | 4 | Player 4 | 1 | 0 | 1 | 2 |
| 2 | Tournament 2 | 1 | Player 1 | 1 | 0 | 0 | 1 |
| 2 | Tournament 2 | 2 | Player 2 | 0 | 1 | 1 | 2 |
| 2 | Tournament 2 | 3 | Player 3 | 0 | 1 | 0 | 1 |
| 2 | Tournament 2 | 5 | Player 5 | 0 | 0 | 0 | 0 |
关于sql - 无法显示获胜的比赛,比赛的进行以及每个球员的比赛,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31484776/