MySQL BETWEEN在unsigned bigint上

时间:2010-09-02 18:07:12

标签: mysql ipv6 bigint

我正在开发具有IPv6模型的rails应用程序。我将IPv6地址存储为2个32位整数和一个64位整数:

+-----------------------+---------------------+------+-----+---------+----------------+
| Field                 | Type                | Null | Key | Default | Extra          |
+-----------------------+---------------------+------+-----+---------+----------------+
| global_routing_prefix | int(11) unsigned    | YES  |     | NULL    |                | 
| subnet_identifier     | int(11) unsigned    | YES  |     | NULL    |                | 
| interface_identifier  | bigint(20) unsigned | YES  |     | NULL    |                | 

不幸的是,当我在一个范围内找到IP时,MySQL会使用签名的bigint进行所有算术运算,所以:

mysql> select 2 BETWEEN 0 AND 18446744073709551614;
+--------------------------------------+
| 2 BETWEEN 0 AND 18446744073709551614 |
+--------------------------------------+
|                                    0 | 
+--------------------------------------+

我可以做些工作,还是需要将interface_identifier拆分为2个无符号整数?

谢谢, 唐纳德

2 个答案:

答案 0 :(得分:2)

丑陋的解决方案:

select 2 BETWEEN 0 AND CAST(18446744073709551614 AS DECIMAL);

MontyAB的人员正在为MariaDB / MySQL开发本机IPv6列类型。如果你能支持他们,我们可能会更快地拥有这个功能。 ;)

答案 1 :(得分:1)

在不使用BETWEEN的情况下执行此操作似乎正常工作

mysql> select 0 <= 2 and 2 <= 18446744073709551614;
+--------------------------------------+
| 0 <= 2 and 2 <= 18446744073709551614 |
+--------------------------------------+
|                                    1 |
+--------------------------------------+