简体   繁体   English

5秒内输入不同

[英]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. 我有一个数据库,我在其中存储条目的unixtime和其他一些东西,在这种情况下,用户的“名称”列和“类型”列可以为1或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. 我想检查是否有名称相同的条目,并在5秒内将类型从1切换到2,然后再切换回1或2 1 2。

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.). 您可以使用该表上的3x自联接和必要条件(所有3行具有相同的名称,等等)来实现。 See http://www.mysqltutorial.org/mysql-self-join/ for more info. 有关更多信息,请参见http://www.mysqltutorial.org/mysql-self-join/

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. 请注意,由于联接会产生所有可能的排列作为输入资料,因此您不必在查询的where部分中对条件进行“排列”。 Eg To get the 5 second rule, you can just say 例如,要获得5秒规则,您可以说

... 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': 编辑:由于原始答案被低估,这是假设表名称为“ 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))

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

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