天天看点

搞定IP地址类型的范围查询

旭东带你学SQL。搞定范围地址的查询需求。

IP地址集合显示-INET_ATON

MariaDB [test]> create table fff ( ipchar(15) );       

Query OK, 0 rows affected (0.03 sec)

MariaDB [test]> insert into fff values('192.168.1.3');

Query OK, 1 row affected (0.02 sec)

MariaDB [test]> select * from fff;

+--------------+

| ip           |

| 192.168.1.3  |

| 192.168.1.4  |

| 192.168.1.5  |

| 192.168.1.6  |

| 192.168.1.7  |

| 192.168.1.8  |

| 192.168.1.9  |

| 192.168.1.10 |

8 rows in set (0.00 sec)

select * from fff whereip>='192.168.1.3' and ip<='192.168.1.10'; 是判断不了的。

所以我们用 INET_ATON函数实现,将IP地址转换为字节后在比较

MariaDB [test]> select * from fff where inet_aton(ip)>=inet_aton('192.168.1.3')and inet_aton(ip)<=inet_aton('192.168.1.10');

8 rows in set (0.01 sec)