一个表有两个字段,想查询那些srcip有对应5个或者5个以上连续dstip
table ip
+-------------+-------------+
| srcip | dstip |
+-------------+-------------+
| 192.168.0.2 | 192.168.1.4 |
| 192.168.0.2 | 192.168.1.3 |
| 192.168.0.2 | 192.168.1.5 |
| 192.168.0.2 | 192.168.1.6 |
| 192.168.0.2 | 192.168.1.7 |
| 192.168.0.2 | 192.168.1.2 |
| 192.168.0.5 | 192.168.1.2 |
+-------------+-------------+
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我想用存储过程写一个。
程序还有待调通。
关键点:
1.用
GROUP BY和HAVING子句找出dstip大于等于5个的记录2.使用INET_ATON函数将dstip转成Integer类型。
说一下思路,SQL忘的差不多了。
为了方便描述,表简化为src为字符串,dist为整数,表称为t。
t join自己 t1,条件为src相等,t1.dist-t.dist in [0,4]
结果为
IPa 1 1
IPa 1 2
……
IPa 1 5
从这个结果group by t.src, t.dist having count(*) = 5