Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003
    Posts
    61

    Unanswered: 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

  2. #2
    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/
    --

  3. #3
    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 :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •