我有一个数据库,其中一列中列出了一堆 IP 地址。我想统计每一个ip地址出现的时间。但我想删除 ip 地址中最后一个句点之后的所有数字并按结果进行分组。因此,对于诸如 192.178.168.2
这样的 IP 地址,我想按 192.178.168
进行分组。同样,192.178.168.234
将按 192.178.168
分组。
如何进行这种类型的字符串操作?
select count(*)
from tbl t
group by t.ip_address
order by count(*) desc
limit 10
;
最佳答案
这在 MySQL 中非常简单,因为有函数 substring_index()
:
select substring_index(t.ip_address, '.', 3) as TypeC, count(*)
from tbl t
group by substring_index(t.ip_address, '.', 3)
order by count(*) desc
limit 10;
这不包括最后一个时期。如果您确实需要它,请将其重新连接起来。
关于MySQL 字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22568894/