更新时间:2024-05-07 GMT+08:00
网络地址函数和操作符
cidr和inet操作符
操作符<<,<<=,>>,>>=对子网进行测试。它们只考虑两个地址的网络部分(忽略任何主机部分),然后判断其中一个网络是等于另外一个网络,还是另外一个网络的子网。
- <
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.5' < inet '192.168.1.6' AS RESULT; result -------- t (1 row)
- <=
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.5' <= inet '192.168.1.5' AS RESULT; result -------- t (1 row)
- =
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.5' = inet '192.168.1.5' AS RESULT; result -------- t (1 row)
- >=
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.5' >= inet '192.168.1.5' AS RESULT; result -------- t (1 row)
- >
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.5' > inet '192.168.1.4' AS RESULT; result -------- t (1 row)
- <>
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.5' <> inet '192.168.1.4' AS RESULT; result -------- t (1 row)
- <<
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.5' << inet '192.168.1/24' AS RESULT; result -------- t (1 row)
- <<=
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1/24' <<= inet '192.168.1/24' AS RESULT; result -------- t (1 row)
- >>
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1/24' >> inet '192.168.1.5' AS RESULT; result -------- t (1 row)
- >>=
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1/24' >>= inet '192.168.1/24' AS RESULT; result -------- t (1 row)
- ~
示例:
1 2 3 4 5
gaussdb=# SELECT ~ inet '192.168.1.6' AS RESULT; result --------------- 63.87.254.249 (1 row)
- &
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.6' & inet '10.0.0.0' AS RESULT; result --------- 0.0.0.0 (1 row)
- |
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.6' | inet '10.0.0.0' AS RESULT; result ------------- 202.168.1.6 (1 row)
- +
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.6' + 25 AS RESULT; result -------------- 192.168.1.31 (1 row)
- -
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.43' - 36 AS RESULT; result ------------- 192.168.1.7 (1 row)
- -
示例:
1 2 3 4 5
gaussdb=# SELECT inet '192.168.1.43' - inet '192.168.1.19' AS RESULT; result -------- 24 (1 row)
cidr和inet函数
函数abbrev,host,text主要是为了提供可选的显示格式。
- abbrev(inet)
返回类型:text
示例:
1 2 3 4 5
gaussdb=# SELECT abbrev(inet '10.1.0.0/16') AS RESULT; result ------------- 10.1.0.0/16 (1 row)
- abbrev(cidr)
返回类型:text
示例:
1 2 3 4 5
gaussdb=# SELECT abbrev(cidr '10.1.0.0/16') AS RESULT; result --------- 10.1/16 (1 row)
- broadcast(inet)
返回类型:inet
示例:
1 2 3 4 5
gaussdb=# SELECT broadcast('192.168.1.5/24') AS RESULT; result ------------------ 192.168.1.255/24 (1 row)
- family(inet)
返回类型:int
示例:
1 2 3 4 5
gaussdb=# SELECT family('127.0.0.1') AS RESULT; result -------- 4 (1 row)
- host(inet)
返回类型:text
示例:
1 2 3 4 5
gaussdb=# SELECT host('192.168.1.5/24') AS RESULT; result ------------- 192.168.1.5 (1 row)
- hostmask(inet)
返回类型:inet
示例:
1 2 3 4 5
gaussdb=# SELECT hostmask('192.168.23.20/30') AS RESULT; result --------- 0.0.0.3 (1 row)
- masklen(inet)
返回类型:int
示例:
1 2 3 4 5
gaussdb=# SELECT masklen('192.168.1.5/24') AS RESULT; result -------- 24 (1 row)
- netmask(inet)
返回类型:inet
示例:
1 2 3 4 5
gaussdb=# SELECT netmask('192.168.1.5/24') AS RESULT; result --------------- 255.255.255.0 (1 row)
- network(inet)
返回类型:cidr
示例:
1 2 3 4 5
gaussdb=# SELECT network('192.168.1.5/24') AS RESULT; result ---------------- 192.168.1.0/24 (1 row)
- set_masklen(inet, int)
返回类型:inet
示例:
1 2 3 4 5
gaussdb=# SELECT set_masklen('192.168.1.5/24', 16) AS RESULT; result ---------------- 192.168.1.5/16 (1 row)
- set_masklen(cidr, int)
返回类型:cidr
示例:
1 2 3 4 5
gaussdb=# SELECT set_masklen('192.168.1.0/24'::cidr, 16) AS RESULT; result ---------------- 192.168.0.0/16 (1 row)
- text(inet)
返回类型:text
示例:
1 2 3 4 5
gaussdb=# SELECT text(inet '192.168.1.5') AS RESULT; result ---------------- 192.168.1.5/32 (1 row)
任何cidr值都能以显式或者隐式的方式转换为inet值,因此上述能够操作inet值的函数也同样能够操作cidr值。inet值也可以转换为cidr值,此时inet子网掩码右侧的所有位都将转换为零,以创建一个有效的cidr值。另外,用户还可以使用常规的类型转换语法将一个文本字符串转换为inet或cidr值。例如:inet(expression)或colname::cidr。
macaddr函数
函数trunc(macaddr)返回一个MAC地址,该地址的最后三个字节设置为零。
macaddr类型还支持标准关系操作符(>,<=等)用于词法排序,和按位运算符(~,&和|)非,与和或。
父主题: 函数和操作符