Wednesday, August 3, 2011

How to sort mysql table by ip address string column

There are lots of result for ordering mysql table by IP address string column on the net. These are almost hellish tricky. So I use this:

ips=Ip.where(:reserved => false).order("INET_ATON(address) ASC")

You can save numeric value of ip address also with INET_ATON, then you can get the string value by "INET_NTOA()" mysql function.

Here is the link about mysql miscellaneous functions:
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

Good luck!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.