MySQL:获取每行的最新操作
我正在尝试编写一个查询,在其中我想获取用户最后一次“特殊操作”的列表,因此我有这些表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你很接近了。尝试:
You're close. Try:
您得到此结果可能是因为您使用了聚合函数 (
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.