Mega Code Archive

 
Categories / MySQL / Select Clause
 

Sorting hostname values correctly in right-to-left fashion

mysql> mysql> CREATE TABLE hostname     -> (     ->  name    VARCHAR(64)     -> ); Query OK, 0 rows affected (0.01 sec) mysql> mysql> INSERT INTO hostname (name)     ->  VALUES     ->          ('cvs.php.net'),     ->          ('dbi.perl.org'),     ->          ('lists.mysql.com'),     ->          ('mysql.com'),     ->          ('jakarta.apache.org'),     ->          ('www.kitebird.com')     -> ; Query OK, 6 rows affected (0.00 sec) Records: 6  Duplicates: 0  Warnings: 0 mysql> mysql> SELECT name FROM hostname     -> ORDER BY     -> SUBSTRING_INDEX(name,'.',-1),     -> SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT('.',name),'.',-2),'.',1),     -> SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT('..',name),'.',-3),'.',1); +--------------------+ | name               | +--------------------+ | www.kitebird.com   | | mysql.com          | | lists.mysql.com    | | cvs.php.net        | | jakarta.apache.org | | dbi.perl.org       | +--------------------+ 6 rows in set (0.00 sec) mysql> mysql> drop table hostname; Query OK, 0 rows affected (0.00 sec)