天天看點

搞定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)