简体   繁体   中英

Different entries within 5 seconds

Sorry I don't know how to describe the topic.

i have a database where i store the unixtime of the entries and some other stuff, in this case the column "name" for the user and "type" it can be 1 or 2.

I want to check if there are entries where name is the same and type switches from 1 to 2 and back to 1 or 2 1 2 within 5 seconds.

So it shows me something like this:

Unixtime   Name type
1550293559 Peter 2
1550293560 Peter 1
1550293561 Peter 2

Is there a query that can help me do this?

Sorry I really hope you guys understand that, I don't know how to explain the problem properly.

Thanks.

You can do that with a 3x self join on that table and the necessary conditions (All 3 rows have the same name etc.). See http://www.mysqltutorial.org/mysql-self-join/ for more info.

Note that as the join produces all the possible permutations as input material, you don't have to 'permute' the conditions in the where part of the query. Eg To get the 5 second rule, you can just say

... where e1.unixtime > e2.unixtime and e2.unixtime > e3.unixtime and e3.unixtime+6 > e1.unixtime ... 

Edit: since the original answer was downwoted, here is the full query (grumble grumble) assuming the table name 'sotest':

SELECT 
*
FROM
sotest e1
    JOIN
sotest e2
    JOIN
sotest e3
WHERE
(e1.name = e2.name AND e2.name = e3.name
    AND e1.unixtime > e2.unixtime
    AND e2.unixtime > e3.unixtime
    AND e3.unixtime + 6 > e1.unixtime)
    AND ((e1.type = 1 AND e2.type = 2
    AND e3.type = 1)
    OR (e1.type = 2 AND e2.type = 1
    AND e3.type = 2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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