[英]Select COUNT of MAX in MySQL
I have two tables: Score table
and Teams table
.我有两个表: Score table
和Teams table
。 They both contain some foreign keys from other tables, but in my case I don't need columns from other tables.它们都包含来自其他表的一些外键,但在我的例子中,我不需要来自其他表的列。
I want to get the team id and points
in a SELECT query
.我想在SELECT query
中获取team id and points
。 The points are made from a COUNT of MAX score of a team per round
. COUNT of MAX score of a team per round
。
Score table
: Score table
:
id ID | round_id round_id | team_id team_id | score分数 |
---|---|---|---|
1085e36b-7621-4634-93e2-c5404108ed23 1085e36b-7621-4634-93e2-c5404108ed23 | 424d4186-4432-4b8d-ab9d-00ee71945cf6 424d4186-4432-4b8d-ab9d-00ee71945cf6 | 752f9ebc-7016-42f2-be90-768711fc3d46 752f9ebc-7016-42f2-be90-768711fc3d46 | 18 18 |
47ff3765-6357-47bc-abb8-6b5adf436546 47ff3765-6357-47bc-abb8-6b5adf436546 | 424d4186-4432-4b8d-ab9d-00ee71945cf6 424d4186-4432-4b8d-ab9d-00ee71945cf6 | 90dd8de4-8ddc-46cd-8b67-d93edbede174 90dd8de4-8ddc-46cd-8b67-d93edbede174 | 7 7 |
b26b1a5b-8606-4c08-b838-30fdbcf98697 b26b1a5b-8606-4c08-b838-30fdbcf98697 | 3fbb49a0-7a8a-4db5-b854-84a9ce7ac2b3 3fbb49a0-7a8a-4db5-b854-84a9ce7ac2b3 | 90dd8de4-8ddc-46cd-8b67-d93edbede174 90dd8de4-8ddc-46cd-8b67-d93edbede174 | 20 20 |
c1add78a-94a3-48cf-b89d-4e4500ab738d c1add78a-94a3-48cf-b89d-4e4500ab738d | 3fbb49a0-7a8a-4db5-b854-84a9ce7ac2b3 3fbb49a0-7a8a-4db5-b854-84a9ce7ac2b3 | 752f9ebc-7016-42f2-be90-768711fc3d46 752f9ebc-7016-42f2-be90-768711fc3d46 | 21 21 |
Teams table
: Teams table
:
id ID | name名称 | tournament_id比赛编号 |
---|---|---|
2b56a499-9fc4-40c2-a7c4-43762c2b27e7 2b56a499-9fc4-40c2-a7c4-43762c2b27e7 | 2@1.com & 4@1.com 2@1.com & 4@1.com | 9aaa6f86-5555-49b8-99a6-900a90dc3c7c 9aaa6f86-5555-49b8-99a6-900a90dc3c7c |
752f9ebc-7016-42f2-be90-768711fc3d46 752f9ebc-7016-42f2-be90-768711fc3d46 | 5@1.com & 3@1.com 5@1.com & 3@1.com | 9aaa6f86-5555-49b8-99a6-900a90dc3c7c 9aaa6f86-5555-49b8-99a6-900a90dc3c7c |
7c9aa32a-ff99-4169-9512-27bc0aac0093 7c9aa32a-ff99-4169-9512-27bc0aac0093 | 7@1.com & 6@1.com 7@1.com & 6@1.com | 9aaa6f86-5555-49b8-99a6-900a90dc3c7c 9aaa6f86-5555-49b8-99a6-900a90dc3c7c |
90dd8de4-8ddc-46cd-8b67-d93edbede174 90dd8de4-8ddc-46cd-8b67-d93edbede174 | 8@1.com & 1@1.com 8@1.com & 1@1.com | 9aaa6f86-5555-49b8-99a6-900a90dc3c7c 9aaa6f86-5555-49b8-99a6-900a90dc3c7c |
As you can see a team have a tournament_id foreign key from a Tournaments table
and Score table have a round_id foreign key from a Rounds table
but I don't need these details in this step.如您所见,一个team have a tournament_id foreign key from a Tournaments table
而Score table have a round_id foreign key from a Rounds table
但我在这一步中不需要这些细节。
As you can see in Score table
, for a round
there are two records, one for each team that plays in that round
and I want to know for any team how many times did it won a round (that's why I'm guessing I need a COUNT of MAX).正如您在Score table
中看到的那样, round
有two records, one for each team that plays in that round
我想知道任何一支球队赢得一轮比赛的次数(这就是为什么我猜我需要MAX 的计数)。
My SELECT query
that I tried:我试过的SELECT query
:
SELECT team_id, count(max(score)) as points FROM Score
WHERE team_id IN (SELECT DISTINCT t.id
FROM Teams t
WHERE t.tournament_id = '9aaa6f86-5555-49b8-99a6-900a90dc3c7c')
GROUP BY round_id
But this query
it throw me the Error Code: 1111. Invalid use of group function .但是这个query
抛出了错误代码:1111。无效使用组 function 。
Expected result:预期结果:
team_id team_id | points积分 |
---|---|
752f9ebc-7016-42f2-be90-768711fc3d46 752f9ebc-7016-42f2-be90-768711fc3d46 | 2 2个 |
90dd8de4-8ddc-46cd-8b67-d93edbede174 90dd8de4-8ddc-46cd-8b67-d93edbede174 | 0 0 |
How am I supposed to get to the desired result?我应该如何达到预期的结果?
Thank you for your time, If something is unclear or there should be more details, let me know!感谢您抽出时间,如果有什么不清楚或应该有更多细节,请告诉我!
If I understand correctly you can try OUTER JOIN
with subquery which get max score by each round_id如果我理解正确,您可以尝试使用子查询进行OUTER JOIN
,每个 round_id 获得最高分
SELECT s.team_id,
COUNT(m_Score)
FROM Score s
LEFT JOIN
(
SELECT round_id,MAX(score) m_Score
FROM Score
GROUP BY round_id
) t1 ON t1.m_Score = s.Score and t1.round_id = s.round_id
WHERE team_id IN (
SELECT t.id
FROM Teams t
WHERE t.tournament_id = '9aaa6f86-5555-49b8-99a6-900a90dc3c7c'
)
GROUP BY s.team_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.