[MYSQL=""]
SELECT title, @a := parentcat_ids, (select title from cats where cat_id = SUBSTRING_INDEX(@a,',',1)) base_title
FROM cats
ORDER BY base_title, parentcat_ids
[/MYSQL]
This gives:
Code:
ASP 12 ASP
Discs 12,10 ASP
Java 12,11 ASP
JSP 3 JSP
PHP 1 PHP
Books 1,5 PHP
XML 1,6 PHP
Ad management 1,7 PHP
Abraham's Report 1,7,13 PHP
XXL 1,7,14 PHP
Zimmick 1,7,14,15 PHP
Hattrick 1,7,14,16 PHP
Junkyfood 1,7,14,17 PHP
Programs 1,7,9 PHP
Ziggurat 1,7,9,18 PHP
Traffic scripts 1,8 PHP
XML's fall 4 XML's fall
This is just almost good! In fact, the sole thing that needs to be done now is to sort the category names in each category.
I've also tried using in the ORDER BY clause:
[MYSQL=""]SUBSTR(parentcat_ids, 1, LENGTH(parentcat_ids) - LOCATE(',', REVERSE(parentcat_ids)))[/MYSQL]
(For example, if parentcat_ids = '10,20,512,63,51', SUBSTR(... gives '10,20,512,63'.)
First thought that it could be of interest but in fact no...
If someone finds out how to acheive this with 1 query (eventually with some subqueries), I am willing to pay you some bucks.