说我有下表:
| Visitor | url | time |
我正在尝试编写以下查询:
for every visitor:
check if visitor has visited /my_url
if yes:
check if visitor visited /my_url/cool url within next 5 min
然后我会有类似的东西:
| visitor | url |
| xyz | /my_url |
| | /my_url/cool |
| abc | /my_url |
| | /my_url/cool |
我不确定从哪里开始。任何帮助将不胜感激。
我到现在所有的一切都是:
select url, time, visitor
from my_table
where url like '%my_url%'
group by visitor;
答案 0 :(得分:1)
尝试这样的事情:
select distinct v1.Visitor
from visits v1
join visits v2
on v2.Visitor = v1.Visitor
and v2.time > v1.time
and v2.time <= v1.time + interval 5 minute
where v1.url = '/my_url'
and v2.url = '/my_url/cool'
由于您在构建查询时知道了网址,因此无需选择它们。
答案 1 :(得分:1)
不是一个非常复杂的查询。
每位访客
这是否意味着您希望未访问过URL的访问者包含在输出中? (我没有找到您的描述或示例清楚)。假设情况就是这样......
据推测,您的表中记录的记录多于唯一身份访问者,因此如果您可以加入访问者表,那么解决方案会更有效率,但如果您没有,则可以使用:
SELECT DISTINCT visitor
FROM yourtable
检查访问者是否访问过[url1]
SELECT ilv.visitor, u1.time
FROM (
SELECT DISTINCT visitor
FROM yourtable
) AS ilv
LEFT JOIN yourtable AS u1
ON u1.visitor=ilv.visitor
AND u1.url=[url1]
检查访问者是否在接下来的5分钟内访问了[url2]
SELECT ilv.visitor, u1.time,
(SELECT MIN(time)
FROM yourtable u2
WHERE u2.visitor=u1.visitor
AND u2.url=[url2]
AND u2.time between u1.time
AND u1.time + INTERVAL 5 MINUTES
)
FROM (
SELECT DISTINCT visitor
FROM yourtable
) AS ilv
LEFT JOIN yourtable AS u1
ON u1.visitor=ilv.visitor
AND u1.url=[url1];
但是,如果您只想要符合这两个标准的访问者列表
SELECT visitor
FROM yourtable u1
JOIN yourtable u2
ON u1.visitor=u2.visitor
AND u2.time between u1.time
AND u1.time + INTERVAL 5 MINUTES
WHERE u1.url=[url1]
AND u2.url=[url2]