gpt4 book ai didi

MySQL 字符串操作

转载 作者:行者123 更新时间:2023-11-29 01:29:14 25 4
gpt4 key购买 nike

我有一个数据库,其中一列列出了一堆 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/

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