嵌套 SQL 查询

发布于 2024-09-25 21:38:22 字数 515 浏览 4 评论 0原文

我有以下表格:

俱乐部:Club_ID |标题 |创建日期 | ...

Club_Intesect:用户 ID |俱乐部_ID |访问

我正在尝试选择可变数量的俱乐部,并加入该俱乐部中具有最高访问权限的用户的 ID。此人被视为所有者。

因此,如果 Club 100 的会员 A、B、C 分别具有访问权限 3、4 和 5:我希望最终查询从俱乐部中选择:

 Club.Club_ID     Club.Title        Club.Created_Date  Club_Intersect.User_ID

      100         |  "Test Club"   |  "Creation Date"     |       C             |
      101         | "Test Club 2"  |  "Creation_Date"     | Highest Access User |
      ...

I have the following tables:

Club: Club_ID | Title | Created_Date | ...

Club_Intesect: User_ID | Club_ID | Access

I'm trying to select a variable number of clubs, and join the ID of the user with the highest access in that club. This person is considered the owner.

So if Club 100 has Members A, B, C with access 3,4, and 5 respectively: I want the final query to select from club:

 Club.Club_ID     Club.Title        Club.Created_Date  Club_Intersect.User_ID

      100         |  "Test Club"   |  "Creation Date"     |       C             |
      101         | "Test Club 2"  |  "Creation_Date"     | Highest Access User |
      ...

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

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

发布评论

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

评论(2

挖鼻大婶 2024-10-02 21:38:22
SELECT *
FROM Club c
JOIN Club_Intesect ci ON ci.Club_ID = c.Club_ID
WHERE ci.Access =
    (SELECT MAX(ACCESS)
     FROM Club_Intesect
     WHERE Club_ID = c.Club_ID)

未经测试,但你明白了

SELECT *
FROM Club c
JOIN Club_Intesect ci ON ci.Club_ID = c.Club_ID
WHERE ci.Access =
    (SELECT MAX(ACCESS)
     FROM Club_Intesect
     WHERE Club_ID = c.Club_ID)

Not tested but you get the idea

破晓 2024-10-02 21:38:22
SELECT *
FROM Club
WHERE CLUB_ID =
    (SELECT Club_ID
     FROM Club_Intersect
     ORDER BY ACCESS DESC
     LIMIT 1);

我认为这可以使用嵌套 SQL 查询来选择您想要的俱乐部。您没有在返回的结果中指定您想要的内容,因为此查询只会检索 Club 列。

SELECT *
FROM Club
WHERE CLUB_ID =
    (SELECT Club_ID
     FROM Club_Intersect
     ORDER BY ACCESS DESC
     LIMIT 1);

I think that would work for selecting the club you want, using a nested SQL Query. You didn't specify what you wanted in your returned results, because this query would only retrieve the Club columns.

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