我使用下面的sql来显示最近5分钟内浏览网站的活跃用户。然后,这些行与所浏览的页面一起显示。但是,如果用户(哈希)在5分钟内浏览了多个页面,则所有页面都将被显示。
我只想显示每个用户的最后一页。我该怎么做呢?限制1不起作用,因为我想显示所有活动用户的最后一个页面。不止一个用户。
SELECT behaviour.hash,
behaviour.page
FROM active
inner join behaviour
ON active.hash = behaviour.hash
WHERE active.last_active >= Now() - interval 10 second
AND behaviour.timestamp >= Now() - interval 5 minute
ORDER BY behaviour.timestamp DESC
我认为这将工作为您正在做的:
SELECT b.hash, b.page
FROM active a join
behaviour b
ON a.hash = b.hash join
(select b2.hash, max(timestamp) as maxts
from behavior b2
where timestamp >= Now() - interval 5 minute
group by b2.hash
) bh
ON bh.hash = b.hash and bh.maxts = b.timestamp
WHERE a.last_active >= Now() - interval 10 second
ORDER BY b.timestamp DESC ;
获取子查询中的最大时间戳并将其连接回。