Hi,

If you convert the IP address into integers, then do an xor (exclusive or),
you could call the resultant value the approximate 'distance' between the
values.

(think of the IP address in binary, any identical bits will result in 0, any
differing will be 1.  This means that the most significant '1' will be the
first dissimilar bit between the two IP addresses.  This is really what you
are after.  However, including any lesser significant bits may well be OK
for your application.)

OK, so how in SQL

You need to create, on the fly

      ipAddress1 ^ ipAddress2

(order does not matter) where they are both integers (mysql will use 64bit
integer arithmetic).

You just need to ORDER BY this value.





Regards


Matthew


-----Original Message-----
From: Ray [mailto:[EMAIL PROTECTED]
Sent: 19 June 2003 15:33
To: [EMAIL PROTECTED]
Subject: wierd sort query, how do you do it? (sort by ip proximity
guess)


currently i have a table with an ip coloumn in text 123.45.67.89
though it shouldn't be a big deal to convert it to binary, but was wondering
if there is an easy way to sort by closeness to a given ip address?

ie records that come out sorted as
same class sub-c
same class c
same class c
same class b
same class a
other addresses
other addresses

the only thing i can think of at this point is a really long
order by <if same c>,<if same b>,<if same a>

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to