簡體   English   中英

如何編寫以下SQL查詢

[英]How to write the following SQL query

我在具有以下架構的MYSQL DB上。

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

我需要寫一個查詢來查找所有URL,在一小時或更長時間內被兩個不同的服務器切換。

因此,例如,如果url1在11:00由server1提供服務,並在12:00由server2提供服務,那么它應該在結果中;但是,如果url2在11:05由server1提供服務,在11:30由server2提供服務,不應該出現在結果中。

請在這里找到樣本小提琴 在這種情況下,輸出應為www.url2.com

只需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演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM