0

在 MySQL 中,我需要选择字段不是 IP 地址的所有行(例如 12.32.243.43)。这只能用 MySQL 完成吗?

例如:我尝试了以下但不匹配。

select * from mytable where field not like '%.%.%.%' 
4

2 回答 2

2

如果不需要使用正则表达式,这将提供解决方案:

select stringBasedIp from x
where inet_aton(stringBasedIp) is null;


select stringBasedIp, (inet_aton(stringBasedIp) is null) as isInvalidIp
from x;

样本数据:

create table x(stringBasedIp varchar(16));

insert into x values
('255.255.255.255'),
('0.0.0.0'),
('0.0.0.300'),
('0.0.0.-1'),
('0.0.0.A'),
('192.168.0.1'),
('400.168.0.1'),
('12.32.243.43'),
('12.32.243.430');

以下是无效ip列表:

STRINGBASEDIP
0.0.0.300
0.0.0.-1
0.0.0.A
400.168.0.1
12.32.243.430

现场测试:http ://www.sqlfiddle.com/#!2/2a4ec/1

于 2012-04-30T02:10:25.577 回答
2

当然可以。你会寻找类似的东西:

SELECT * FROM `table` WHERE NOT( some_field REGEXP '^[0-9+]\.[0-9]+\.[0-9+]\.[0-9+]')
于 2012-04-29T05:03:17.820 回答