查询一对多对多表关系的最有效方法

发布于 2024-12-14 00:35:29 字数 704 浏览 1 评论 0原文

我有 3 个描述游戏的 MySQL 表:

  1. gamelist {gameId, ... with Primary(gameId) } = 存储有关每个游戏会话的信息。
  2. userlist {userId, ... with Primary(userId) } = 存储每个用户的信息。
  3. game2user {gameId, userId ... with Primary(gameId, userId) } = 存储参与每个游戏会话的用户(一个游戏会话涉及两个或多个用户)。

现在,对于某个 userId,我需要检索一个游戏列表,其中包含该游戏会话中涉及的所有用户的名称。

到目前为止我得到的最好的结果是:

SELECT gl.gameid, gl.type, [...], ul.userid, ul.username
FROM userlist as ul, gamelist as gl, game2users as g2u
WHERE g2u.userid = a_certain_numeric_id AND
      ul.userid = g2u.userid AND
      gl.gameid = g2u.gameid`

这从游戏列表中检索唯一的行,但它只获取给定用户的用户名,而不是他的同事。

问题是如何在性能(时间)方面有效地做到这一点,并且最好不进行单独的查询?谢谢。

I have 3 MySQL tables that describe a game:

  1. gamelist {gameId, ... with Primary(gameId) } = Stores information about each game session.
  2. userlist {userId, ... with Primary(userId) } = Stores each user's information.
  3. game2user {gameId, userId ... with Primary(gameId, userId) } = Stores which users are involved in each game session (one game session to two or more users).

Now, for a certain userId, I need to retrieve a list of games that include the names of all the users involved in that game session.

The best I came out with so far is:

SELECT gl.gameid, gl.type, [...], ul.userid, ul.username
FROM userlist as ul, gamelist as gl, game2users as g2u
WHERE g2u.userid = a_certain_numeric_id AND
      ul.userid = g2u.userid AND
      gl.gameid = g2u.gameid`

This retrieves the unique rows from games list, but it only fetches the given user's username and not his co-players.

The question is how can I do this efficiently in terms of performance (time) and preferably without doing separate queries? Thanks.

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

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

发布评论

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

评论(3

月光色 2024-12-21 00:35:29
SELECT gl.gameid, gl.type, [...], ul.userid, ul.username
FROM userlist as ul, gamelist as gl, game2users as g2u
WHERE ul.userid = g2u.userid AND
      g2u.gameid IN (SELECT gameid
                     FROM game2users
                     WHERE userid = a_certain_numeric_id);

这本质上是查找给定用户参与的所有游戏(在子查询中),然后选择也参与该游戏的所有用户。

SELECT gl.gameid, gl.type, [...], ul.userid, ul.username
FROM userlist as ul, gamelist as gl, game2users as g2u
WHERE ul.userid = g2u.userid AND
      g2u.gameid IN (SELECT gameid
                     FROM game2users
                     WHERE userid = a_certain_numeric_id);

This essentially looks for all games where the given user is involved (in the subquery) and then selects all users that are also involved in that games.

尸血腥色 2024-12-21 00:35:29

试试这个:

SELECT gl.gameid, gl.type, ..., ul.userid, ul.username
FROM
    (
        SELECT gameid
        FROM game2users
        WHERE userid = a_certain_numeric_id
    ) AS ug
    INNER JOIN gamelist AS gl
        ON ug.gameid = gl.gameid
    INNER JOIN game2users AS g2u
        ON ug.gameid = g2u.gameid
    INNER JOIN userlist AS ul
        ON g2u.userid = ul.userid

Try this:

SELECT gl.gameid, gl.type, ..., ul.userid, ul.username
FROM
    (
        SELECT gameid
        FROM game2users
        WHERE userid = a_certain_numeric_id
    ) AS ug
    INNER JOIN gamelist AS gl
        ON ug.gameid = gl.gameid
    INNER JOIN game2users AS g2u
        ON ug.gameid = g2u.gameid
    INNER JOIN userlist AS ul
        ON g2u.userid = ul.userid
君勿笑 2024-12-21 00:35:29

表和关系的完整架构将帮助我们为您提供帮助。

我不是 MySQL 专家,但似乎您使用不同的数据库实现可能会更好(如果您可以更改它)。现在 games2user 表中有冗余记录。我认为最简单的事情是为用户表提供一个包含他们参与的游戏的属性。然后,当您查询此问题时,您只需在 gamesTable 中查询用户参与的特定游戏并检索该游戏涉及的所有用户 ID。

The full schema for the tables and relationships would help us help you.

I'm not an expert on MySQL but it seems like you may be better off with a different implementation of the database (if you can change that). Right now you have redundant records in games2user table. I think the easiest thing would be to give the user table an attribute that contains the game they are involved in. Then when you do your query for this problem you can just query the gamesTable for the specific game that the user is involved in and retrieve all of the userIDs involved in that game.

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