我有一个Chrome扩展程序,允许具有扩展程序的用户在特定网站上查找其他用户。每隔五分钟,安装了chrome扩展程序的每个用户都将登录log
表,并且每个拥有它们的用户(无论是否具有扩展名)都将记录在seen_log
表中。< / p>
以下是表格的简略视图:
log
|-----------------------------------------------|
| id | timestamp | userid | username | location |
|-----------------------------------------------|
seen_log
|----------------------------------------------------------------------|
| id | ip_address | timestamp | userid | username | location | version |
|----------------------------------------------------------------------|
following
|------------------------------------------------|
| id | timestamp | root_userid | followed_userid |
|------------------------------------------------|
我需要提出一个mysql select语句,它将执行以下操作:
timestamp > date_sub(utc_timestamp(), interval 5 minute) as online
)以及他们是否安装了扩展程序(即如果它们存在于log
表中)简而言之,这是预期的结构:
|------------------------------------------------------------------------|
| followed_userid | followed_username | location | online | hasExtension |
|------------------------------------------------------------------------|
我很难过。你能给我一些指示吗?
编辑:
具体来说,这是我面临的一个问题。我需要使用最新的seen_log
元组加入后面的userid列表。为什么这不能让我获得userid
表中每个唯一seen_log
的最新条目?
select k.*, k.timestamp > date_sub(utc_timestamp(), interval 5 minute) as online
from (
select MAX(id) as max_id, e.*
from seen_log e
group by userid
) k
where k.id=k.max_id
答案 0 :(得分:1)
这应该做的工作
select
fol.root_userid as root,
root.username,
root.location,
fol.followed_userid as follower,
follower.username,
follower.location,
case when follower.timestamp > date_sub(utc_timestamp(), interval 5 minute) then 'online'
else 'offline' end as is_online,
case when fol.followed_userid in (select id from log) then 'registered'
else 'guest' end as is_registered
from following fol
join log root on (root.id=fol.root_userid)
join seen_log follower on (follower.id=fol.followed_userid)
order by fol.root_userid;
答案 1 :(得分:0)
这有效:
select
*,
timestamp > date_sub(utc_timestamp(), interval 5 minute) as is_online,
userid in (select userid from log) as is_registered
from seen_log a
join (
select max(id) as max_id
from seen_log
group by userid
) b
on a.id=b.max_id
where userid in (select followed_userid from following where root_userid = /* user id */ )