gpt4 book ai didi

sql - PostgreSQL - 左连接错误计数输出

转载 作者:行者123 更新时间:2023-11-29 12:33:13 25 4
gpt4 key购买 nike

我正在尝试设置一组简单的表格来显示锦标赛的结果 - 我有以下结构:

CREATE TABLE players(
id SERIAL PRIMARY KEY,
name TEXT);

CREATE TABLE matches(
id SERIAL PRIMARY KEY,
player_one_id INTEGER REFERENCES players,
player_two_id INTEGER REFERENCES players,
winner_id INTEGER REFERENCES players);

并且我输入了一些测试数据,如下:

INSERT INTO players (name) VALUES ('Mike Jones');
INSERT INTO players (name) VALUES ('Albert Awesome');
INSERT INTO players (name) VALUES ('Sad Sally');
INSERT INTO players (name) VALUES ('Lonely Lenny');

INSERT INTO matches (player_one_id, player_two_id, winner_id) VALUES (1,2,1);
INSERT INTO matches (player_one_id, player_two_id, winner_id) VALUES (3,4,4);

我正在尝试执行一个查询,为每个玩家提供以下结果:

id, name, matched_won, matches_played.

到目前为止,我有以下查询:

SELECT players.id, players.name, count(matches.winner_id) as matches_won
, count(matches.id) as matches_played
FROM players left join matches
ON players.id = matches.winner_id
GROUP BY players.id
ORDER BY matches_won DESC

而且,不幸的是,我得到如下不正确的输出(每个玩家应该有 1 个 matches_played):

 id |      name      | matches_won | matches_played 
----+----------------+-------------+----------------
4 | Lonely Lenny | 1 | 1
1 | Mike Jones | 1 | 1
2 | Albert Awesome | 0 | 0
3 | Sad Sally | 0 | 0
(4 rows)

现在,我知道这个错误输出的原因是因为加入了 players.id = matches.winner_id,但是,我的问题是:

是否有可能只用 一个 左连接查询得到这些结果?如果是这样,如何?如果可能,我想避免进行多次查询。

最佳答案

是的。首先,您需要了解 count() 只是计算具有非 NULL 值的行数,因此您的两个计数应该相同。

要获得获胜者,请使用条件聚合:

SELECT p.id, p.name,
sum(case when m.winner_id = p.id then 1 else 0 end) as matches_won,
count(m.id) as matches_played
FROM players p left join
matches m
ON p.id in (m.player_one_id, m.player_two_id)
GROUP BY p.id
ORDER BY matches_won DESC;

您还需要修复 join 条件。您不能只加入获胜者并期望获得所有比赛的计数。

关于sql - PostgreSQL - 左连接错误计数输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28478014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com