简体   繁体   English

如何编写以下SQL查询

[英]How to write the following SQL query

I am on a MYSQL DB with the following schema. 我在具有以下架构的MYSQL DB上。

id bigint(20)
url varchar(500)
timestamp datetime
server varchar(500)

I need to write a query to find all which url gets switched by two different servers in an hour or more. 我需要写一个查询来查找所有URL,在一小时或更长时间内被两个不同的服务器切换。

So for instance if url1 gets served by server1 at 11:00 and gets served by server2 at 12:00 then it should be in the result, however if url2 gets served by server1 at 11:05 and by server2 at 11:30 then it should not be in the result. 因此,例如,如果url1在11:00由server1提供服务,并在12:00由server2提供服务,那么它应该在结果中;但是,如果url2在11:05由server1提供服务,在11:30由server2提供服务,不应该出现在结果中。

Please find the sample fiddle here Fiddle . 请在这里找到样本小提琴 In this case the output should be www.url2.com . 在这种情况下,输出应为www.url2.com

just use timestampdiff function in a where exists...subquery : 只需where exists...subquerywhere exists...subquery使用timestampdiff函数where exists...subquery

select distinct url 
from sample s1
where exists (
     select url 
     from sample s2 
     where 
     s2.url=s1.url
     and timestampdiff(HOUR, s2.timestamp, s1.timestamp)>=1
    )

SQLFIDDLE DEMO SQLFIDDLE演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM