Order by in Postgresql to sort IP address?
I wish to sort a list of IP address.
Is there any function in Postgresql to use with the order by like INET_ATON in MySql ?
My current IP is 开发者_运维知识库on string format.
You can order your IP address column IP_Address
with something like this:
SELECT * FROM MyTable ORDER BY inet(IP_Address)
See the documnetation for further reference.
Edit:
This is actually not a function but a type cast to the postgres integrated type for IP addresses.
I had to do something similar with IPv4 addresses stored as integers in a table, and was able
to use the '0.0.0.0'::inet + num
trick.
So where devaddr
is a column in a table interface
I did:
select distinct ('0.0.0.0'::inet + devaddr) as addr from interface order by addr;
精彩评论