MySQL:获取每行的最新操作

发布于 2024-10-06 21:21:34 字数 1952 浏览 2 评论 0原文

我正在尝试编写一个查询,在其中我想获取用户最后一次“特殊操作”的列表,因此我有这些表:

table_users
id     username     email          etc.
1      moloc        [email protected]     etc.
2      lilly        [email protected]     etc.
3      sammm        [email protected]     etc.


table_special_actions
id     user_id      action_id      date
1      3            25             2010-11-01 22:51:56
2      3            37             2010-11-02 18:09:33
3      3            265            2010-11-03 07:20:12


table_actions
id        action
1         Open a secret
2         Level up
3         Open magic door
etc.      etc.

我的目标是获取一个用户列表,其中每个用户只有最后一次特殊操作,所以我想到了这样的事情:

SELECT *
FROM table_users AS users
LEFT JOIN (
    SELECT user_id, action_id, MAX(date) AS action_date
    FROM table_special_actions
    GROUP BY user_id
    ORDER BY action_date
) s_actions
ON s_actions.user_id = users.user_id
LEFT JOIN table_actions AS actions
ON s_actions.action_id = actions.id

这不能正常工作,因为它返回这个:

-----users-----    ------------s_actions------------    -----actions-----
id     username    action_id     date                   action
3      sammm       25            2010-11-03 07:20:12    Action linked to action_id 25

我期望得到这个:

-----users-----    ------------s_actions------------    -----actions-----
id     username    action_id     date                   action
3      sammm       265           2010-11-03 07:20:12    Action linked to action_id 265

奇怪的是action_id,我总是得到最后一个日期,这没关系,但我'我们也总是获得第一个action_id,而不是相对于正确日期行的action_id。

我哪里错了?

I'm trying to write a query where I'd like to get a list of users's last "special actions", so I have these tables:

table_users
id     username     email          etc.
1      moloc        [email protected]     etc.
2      lilly        [email protected]     etc.
3      sammm        [email protected]     etc.


table_special_actions
id     user_id      action_id      date
1      3            25             2010-11-01 22:51:56
2      3            37             2010-11-02 18:09:33
3      3            265            2010-11-03 07:20:12


table_actions
id        action
1         Open a secret
2         Level up
3         Open magic door
etc.      etc.

My objective is to get an user list where I have only the last special action for each one, so I've thought something like this:

SELECT *
FROM table_users AS users
LEFT JOIN (
    SELECT user_id, action_id, MAX(date) AS action_date
    FROM table_special_actions
    GROUP BY user_id
    ORDER BY action_date
) s_actions
ON s_actions.user_id = users.user_id
LEFT JOIN table_actions AS actions
ON s_actions.action_id = actions.id

This doesn't work properly because it's return this:

-----users-----    ------------s_actions------------    -----actions-----
id     username    action_id     date                   action
3      sammm       25            2010-11-03 07:20:12    Action linked to action_id 25

I've expected to get this:

-----users-----    ------------s_actions------------    -----actions-----
id     username    action_id     date                   action
3      sammm       265           2010-11-03 07:20:12    Action linked to action_id 265

The strange thing is the action_id, I always get the last date and this is ok, but I've also always get the fist action_id instead of the one relative to the correct date row.

Where I'm wrong?

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

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

发布评论

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

评论(2

青朷 2024-10-13 21:21:34

你很接近了。尝试:

SELECT *
    FROM table_users AS users
        LEFT JOIN (SELECT user_id, MAX(date) AS MaxDate
                       FROM table_special_actions
                       GROUP BY user_id) s_actions
            ON s_actions.user_id = users.user_id
        LEFT JOIN table_special_actions s2_actions
            ON s_actions.userid = s2_actions.id
                AND s_actions.MaxDate = s2_actions.date
        LEFT JOIN table_actions AS actions
            ON s2_actions.action_id = actions.id

You're close. Try:

SELECT *
    FROM table_users AS users
        LEFT JOIN (SELECT user_id, MAX(date) AS MaxDate
                       FROM table_special_actions
                       GROUP BY user_id) s_actions
            ON s_actions.user_id = users.user_id
        LEFT JOIN table_special_actions s2_actions
            ON s_actions.userid = s2_actions.id
                AND s_actions.MaxDate = s2_actions.date
        LEFT JOIN table_actions AS actions
            ON s2_actions.action_id = actions.id
潦草背影 2024-10-13 21:21:34

您得到此结果可能是因为您使用了聚合函数 (MAX) 并仅按第一列 (user_id) 进行分组,而保留了第二列 (action_id) code>) 脱离分组和聚合。

You got this result probably because you used an aggregate function (MAX) and grouped only by the first column (user_id) and left the second column (action_id) out of grouping and aggregation.

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