If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Sorting IP addresses

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-29-04, 01:09
noamkrief noamkrief is offline
Registered User
 
Join Date: Dec 2003
Posts: 61
Sorting IP addresses

I am creating an IP inventory database.

On a select query when i try to "oreder by" the ip address it doesn't sort correctly.

if you have the IP addresses in the this format : 172.016.007.020 it sorts fine.

But i would like to be able to view and enter in new IP's in this format:
172.16.7.20

Any clues?
thanks very much
Noam
Reply With Quote
  #2 (permalink)  
Old 05-31-04, 07:30
omiossec omiossec is offline
Registered User
 
Join Date: Jan 2003
Location: Paris, France
Posts: 320
Why not try to use hexa format to sort ip adresse ?
__________________
Olivier Miossec
--
http://www.lasso-developpeur.net/
--
Reply With Quote
  #3 (permalink)  
Old 06-02-04, 10:23
willi.st willi.st is offline
Registered User
 
Join Date: Feb 2004
Location: Vienna/Austria
Posts: 7
Sorting IP-Adresses

From O'Reillys MySQL Cookbook:

SELECT ip FROM hostip
ORDER BY
SUBSTRING_INDEX(ip, '.', 1) + 0,
SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', -3), '.', 1) + 0,
SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', -2), '.', 1) + 0,
SUBSTRING_INDEX(ip, '.', -1) + 0;

or (MySQL 3.23.15 an above)

SELECT ip FROM hostip
ORDER BY INET_ATON(ip);



Willi :-)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On