【发布时间】:2017-04-26 18:38:47
【问题描述】:
我在基本查询中有这种奇怪的行为。
将此视为 twitter 提要,我试图从用户关注的十个不同的人那里获取最后的操作。
select DISTINCT performed_by_user_id, action_id from z_follows, actions
WHERE user_id_following = 1 and z_follows.follower_id = actions.performed_by_user_id
ORDER BY action_id DESC
LIMIT 10
然而,这会返回以下内容(明显不明显):
我也尝试过 group by,但由于 order by,它也会返回不需要的数据。 为什么?
我不能使用 GROUP BY,因为 ORDER BY 会返回不正确的数据。
此时查询返回:
但我希望每个用户只执行一个操作,正如您在上面的屏幕截图中看到的那样,我应该在结果 2、236、685 中看到以下用户 ID,但是这个查询:
select performed_by_user_id, action_id from z_follows, actions
WHERE user_id_following = 1 and z_follows.follower_id = actions.performed_by_user_id
GROUP BY performed_by_user_id
ORDER BY action_id DESC
LIMIT 10
返回这个:
【问题讨论】:
-
不同的适用于结果集而不是单个列。
-
你正在寻找一个群组。
-
@rlanvin group by 也因为 order by 返回了不正确的数据
-
@Callombert 在这种情况下,您必须进一步编辑您的问题以提供您期望的结果集示例。在我看来,您需要 group by 和 max() 以及可能的 join。
-
谢谢,我已经添加了东西@rlanvin
标签: mysql