gpt4 book ai didi

MySQL 根据连续行值和条件选择用户 ID

转载 作者:行者123 更新时间:2023-11-29 20:43:57 24 4
gpt4 key购买 nike

我有一个 MySQL 表

  • row_id(主要)
  • 用户 ID
  • 用户IP
  • date_start(unix 时间戳)
  • date_end(unix 时间戳)

我只想选择此表的用户 ID(不同)IF 其中至少有 2 个连续行

the user_ip in [row X] is different with the user_ip in [row X+1]

ABS( date_end in [row X] - date_start in [row X+1] ) <= 20

行应该按 row_id ASC 排序,以便我们可以连续获取行 X、X+1 等。

我用 PHP 很容易做到了这一点,但是它非常非常慢,所以我认为只使用 sql 就可以完成。

示例

row_id  |  user_id  |  user_ip   |  date_start  |  date_end

1 , 1 , 127.0.0.1 , 1469100096 , 1469100099
2 , 2 , 5.5.5.5 , 1469100054 , 1469100055
3 , 3 , 4.4.4.4 , 1469100032 , 1469100036
4 , 1 , 6.6.6.6 , 1469100117 , 1469100099
5 , 4 , 127.0.0.1 , 1469100001 , 1469100005
6 , 2 , 127.0.0.1 , 1469100555 , 1469100565

由此,我们应该只得到用户 ID 1,因为用户 id 1 在 2 个连续行中具有不同的 IP + 第一行的 date_end (1469100099) - 下一行的日期开始 1(469100117) = 18这是 <= 20。

我们不应该返回用户 ID 2,因为虽然它验证了第一个条件,但它不会验证带有 date_end 和 date_start 的第二个条件。

最佳答案

SELECT parent.* FROM userip parent  
JOIN userip child ON parent.`user_ip` != child.`user_ip`
WHERE ABS( parent.`date_end` - child.date_start) <= 20
AND parent.user_id = child.user_id
GROUP BY parent.user_id

关于MySQL 根据连续行值和条件选择用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38502822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com