带分组的查询设计

发布于 2024-12-26 13:30:19 字数 796 浏览 1 评论 0原文

我无法针对这种情况提出有效的查询...

表格:

[ matchID ]  [ User ] [ team ]
     1          10       1
     1          77       2
     2          10       1
     2          77       1
     3          10       2
     4          10       1

注释:

  • 有时对手是未知的,例如在比赛 2、3 和4
  • 有时一个团队只有一个人,例如比赛 1
  • 有时一个团队有 n 个人,例如比赛 2

期望的查询结果:

给定用户的 matchID 列表,其中对手未知。

我的第一次尝试是:

SELECT matchID FROM table
GROUP BY matchID
HAVING COUNT(matchID) = 1

但后来我意识到我没有统计有队友但对手未知的比赛,例如上面的比赛2。

用简单的英语来说,我需要获取所有 matchID,其中:

  • 涉及给定用户 <- 编辑添加
  • 每个人都在同一个团队中
  • ,或者只列出一个人

,但我不知道如何在一个查询中有效地做到这一点。帮助?

I'm having trouble coming up with a working query for this situation...

Table:

[ matchID ]  [ User ] [ team ]
     1          10       1
     1          77       2
     2          10       1
     2          77       1
     3          10       2
     4          10       1

Notes:

  • sometimes the opponent is unknown, for example in matches 2, 3, and 4
  • sometimes a team has only one person on a side, for example match 1
  • sometimes a team has n persons on a side, for example match 2

Desired Query Results:

A list of matchIDs for a given user where the opponent is unknown.

My first attempt was:

SELECT matchID FROM table
GROUP BY matchID
HAVING COUNT(matchID) = 1

but then I realized that I wasn't counting matches where there are teammates but unknown opponents, such as match 2 above.

In plain English, I need to get all matchIDs where:

  • involving a given user <- edited to add
  • everyone is on the same team
  • OR there is only one person listed

but I'm not sure how to do that efficiently in one query. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅此而已 2025-01-02 13:30:20

您可以计算每场比赛的球队数量。如果只有一个那就不够了:

SELECT matchID 
FROM table
GROUP BY matchID 
HAVING COUNT(DISTINCT(team))=1;

You can count the number of teams per match. If there's only one then there's not enough:

SELECT matchID 
FROM table
GROUP BY matchID 
HAVING COUNT(DISTINCT(team))=1;
白色秋天 2025-01-02 13:30:20

您尚未提供表名称,因此我决定将其命名为 mut 作为 matches users team 的缩写。无论如何,这似乎适用于所有情况:

SELECT DISTINCT
    `matchID`
FROM
    `mut`
WHERE
    `user`=10
AND
    `matchID` NOT IN (
        SELECT
            `m1`.`matchID`
        FROM
            `mut` AS `m1`
        INNER JOIN
            `mut` AS `m2`
        ON
            `m1`.`matchID`=`m2`.`matchID`
        AND
            `m1`.`team`<>`m2`.`team`
    )

you haven't provided a table name, so i have decided to call it mut as in short for matches users teams. anyways, this seems to be working on all cases:

SELECT DISTINCT
    `matchID`
FROM
    `mut`
WHERE
    `user`=10
AND
    `matchID` NOT IN (
        SELECT
            `m1`.`matchID`
        FROM
            `mut` AS `m1`
        INNER JOIN
            `mut` AS `m2`
        ON
            `m1`.`matchID`=`m2`.`matchID`
        AND
            `m1`.`team`<>`m2`.`team`
    )
夜夜流光相皎洁 2025-01-02 13:30:19
SELECT MatchID
  FROM Table
 GROUP BY MatchID
HAVING COUNT(DISTINCT Team) = 1;

您似乎需要那些表中只记录了一支球队的比赛,并且此查询为您提供了这些比赛。

如果匹配必须涉及特定用户(示例数据为 10),那么:

SELECT t1.MatchID
  FROM Table AS t1
 WHERE EXISTS(SELECT *
                FROM Table AS t2
               WHERE t2.UserID = 10 AND t1.MatchID = t2.MatchID)
 GROUP BY t1.MatchID
HAVING COUNT(DISTINCT t1.Team) = 1;

您可以将其作为(内部)自连接来执行:

SELECT t1.MatchID
  FROM Table AS t1
  JOIN Table AS t2 ON t1.MatchID = t2.MatchID AND t2.UserID = 10
 GROUP BY t1.MatchID
HAVING COUNT(DISTINCT t1.Team) = 1;

或者,也许更清楚一点:

SELECT t1.MatchID
  FROM Table AS t1
  JOIN (SELECT MatchID FROM Table AS t2 WHERE t2.UserID = 10) AS t2
    ON t1.MatchID = t2.MatchID
 GROUP BY t1.MatchID
HAVING COUNT(DISTINCT t1.Team) = 1;

子选择选择 UserID 10 的那些匹配玩过;其余的工作和以前一样。

SELECT MatchID
  FROM Table
 GROUP BY MatchID
HAVING COUNT(DISTINCT Team) = 1;

You seem to need those matches where only one team is recorded in the table, and this query gives you those matches.

If the match must involve a specific user (10 for the sample data), then:

SELECT t1.MatchID
  FROM Table AS t1
 WHERE EXISTS(SELECT *
                FROM Table AS t2
               WHERE t2.UserID = 10 AND t1.MatchID = t2.MatchID)
 GROUP BY t1.MatchID
HAVING COUNT(DISTINCT t1.Team) = 1;

You can probably do that as an (inner) self-join:

SELECT t1.MatchID
  FROM Table AS t1
  JOIN Table AS t2 ON t1.MatchID = t2.MatchID AND t2.UserID = 10
 GROUP BY t1.MatchID
HAVING COUNT(DISTINCT t1.Team) = 1;

Or, perhaps a little more clearly:

SELECT t1.MatchID
  FROM Table AS t1
  JOIN (SELECT MatchID FROM Table AS t2 WHERE t2.UserID = 10) AS t2
    ON t1.MatchID = t2.MatchID
 GROUP BY t1.MatchID
HAVING COUNT(DISTINCT t1.Team) = 1;

The sub-select picks those matches where UserID 10 played; the rest works as before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文