sorting output by incrementing IP in mysql I want an incrementing list of IP #'s in a subnet to display on a php web page in a table. Right now, 10.1.2.3 would display after 10.1.2.10 in the sequence. 

My current query is this:

SELECT * FROM `ip` WHERE `subnet` LIKE '10.1.2.0/24' ORDER BY ip;


What query do I need? 

My IP field is 16 character VARCHAR.
You can sort this using INET_ATON:

Query:

SELECT * FROM `ip` WHERE `subnet` LIKE '10.1.2.0/24' ORDER BY INET_ATON(ip);


Data:

mysql&gt; select * from ip;
+------------+-------------+
| ip         | subnet      |
+------------+-------------+
| 10.1.2.1   | 10.1.2.0/24 |
| 10.1.2.10  | 10.1.2.0/24 |
| 10.1.2.123 | 10.1.2.0/24 |
| 10.1.2.3   | 10.1.2.0/24 |
+------------+-------------+
4 rows in set (0.00 sec)


Test:

mysql&gt; SELECT * FROM `ip` WHERE `subnet` LIKE '10.1.2.0/24' ORDER BY INET_ATON(ip);
+------------+-------------+
| ip         | subnet      |
+------------+-------------+
| 10.1.2.1   | 10.1.2.0/24 |
| 10.1.2.3   | 10.1.2.0/24 |
| 10.1.2.10  | 10.1.2.0/24 |
| 10.1.2.123 | 10.1.2.0/24 |
+------------+-------------+
4 rows in set (0.00 sec)

mysql&gt;