mysql 查询必须如何才能达到显示的结果?

发布于 2024-08-19 00:30:30 字数 987 浏览 2 评论 0原文

比如说,我有两个这样的表:

Table group         Table user
+----+-----------+  +----+----------+------+----------+
| id | groupname |  | id | username | rank | group_id |
+----+-----------+  +----+----------+------+----------+
|  1 | Friends   |  |  1 | Frank    |    1 |        1 |
|  2 | Family    |  |  2 | Mike     |    3 |        1 |
+----+-----------+  |  3 | Steve    |    2 |        1 |
                    |  4 | Tom      |    1 |        2 |
                    +----+----------+------+----------+

我想选择所有组并获取每个组中排名最高(最高数字)的用户。所以基本上我想得到这个结果:

+-----------------+----------+---------+---------------+    
| group.groupname | group.id | user.id | user.username |
+-----------------+----------+---------+---------------+
| Friends         |        1 |       2 |         Mike  |
| Family          |        2 |       4 |          Tom  |
+-----------------+----------+---------+---------------+    

选择怎么样? 也许很简单,但我现在不明白......

Say, I have two tables like these:

Table group         Table user
+----+-----------+  +----+----------+------+----------+
| id | groupname |  | id | username | rank | group_id |
+----+-----------+  +----+----------+------+----------+
|  1 | Friends   |  |  1 | Frank    |    1 |        1 |
|  2 | Family    |  |  2 | Mike     |    3 |        1 |
+----+-----------+  |  3 | Steve    |    2 |        1 |
                    |  4 | Tom      |    1 |        2 |
                    +----+----------+------+----------+

And I want to select all groups and get the user with the highest rank (the highest number) for each group. So basically I want to get this result:

+-----------------+----------+---------+---------------+    
| group.groupname | group.id | user.id | user.username |
+-----------------+----------+---------+---------------+
| Friends         |        1 |       2 |         Mike  |
| Family          |        2 |       4 |          Tom  |
+-----------------+----------+---------+---------------+    

How has the select to be like?
It maybe very simple, but I'm not getting it right now....

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

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

发布评论

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

评论(2

┈┾☆殇 2024-08-26 00:30:30

编辑2:

我之前的答案是错误的,max()调用破坏了结果。这是一个正确的解决方案:

SELECT g.groupname, g.id AS group_id, u.id AS user_id, u.username
FROM `user` u
LEFT JOIN `group` g ON (u.group_id=g.id)
WHERE u.rank=(
    SELECT MAX(rank) 
    FROM `user` u2 
    WHERE u.group_id=u2.group_id
)

WHERE 子句中的检查也应该更容易理解。

mysql> SELECT g.groupname, g.id AS group_id, u.id AS user_id, u.username
    -> FROM `user` u
    -> LEFT JOIN `group` g ON (u.group_id=g.id)
    -> WHERE u.rank=(
    ->     SELECT MAX(rank)
    ->     FROM `user` u2
    ->     WHERE u.group_id=u2.group_id
    -> );
+-----------+----------+---------+----------+
| groupname | group_id | user_id | username |
+-----------+----------+---------+----------+
| Friends   |        1 |       2 | Mike     |
| Family    |        2 |       4 | Tom      |
+-----------+----------+---------+----------+
2 rows in set (0.00 sec)

Edit 2:

My previous answer was wrong, the max() call destroyed the result. Here's a correct solution:

SELECT g.groupname, g.id AS group_id, u.id AS user_id, u.username
FROM `user` u
LEFT JOIN `group` g ON (u.group_id=g.id)
WHERE u.rank=(
    SELECT MAX(rank) 
    FROM `user` u2 
    WHERE u.group_id=u2.group_id
)

The check in the WHERE clause should be more understandable, too.

mysql> SELECT g.groupname, g.id AS group_id, u.id AS user_id, u.username
    -> FROM `user` u
    -> LEFT JOIN `group` g ON (u.group_id=g.id)
    -> WHERE u.rank=(
    ->     SELECT MAX(rank)
    ->     FROM `user` u2
    ->     WHERE u.group_id=u2.group_id
    -> );
+-----------+----------+---------+----------+
| groupname | group_id | user_id | username |
+-----------+----------+---------+----------+
| Friends   |        1 |       2 | Mike     |
| Family    |        2 |       4 | Tom      |
+-----------+----------+---------+----------+
2 rows in set (0.00 sec)
森林散布 2024-08-26 00:30:30
select g.groupname, u.group_id, u.id as user_id, u.username
from group g
inner join (
    select group_id, max(rank) as MaxRank
    from user
    group by group_id
) um on g.id = um.group_id
inner join user u on um.group_id = u.group_id and um.MaxRank = u.rank
select g.groupname, u.group_id, u.id as user_id, u.username
from group g
inner join (
    select group_id, max(rank) as MaxRank
    from user
    group by group_id
) um on g.id = um.group_id
inner join user u on um.group_id = u.group_id and um.MaxRank = u.rank
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文