'Mysql - SELECT Parent-Child with SORT

I have one level parent child relation table, with following columns:

ID| Parent_ID | ProductTitle

I need output grouped by Parent followed by children, and also sorted by Parent and Children Name.

I found a working example for SQL Server, but I need something that works on MYSQL. https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=48bab28f42327d41d11df9c6795c18a8

Please help.



Solution 1:[1]

SELECT t1.ID_Asset, 
       t1.Parent_ID_Asset, 
       t1.ProductTitle
FROM Test t1
LEFT JOIN Test t2 ON t2.ID_Asset = t1.Parent_ID_Asset
ORDER BY COALESCE(t2.ProductTitle, t1.ProductTitle), 
         t1.Parent_ID_Asset IS NOT NULL, 
         t1.ProductTitle

https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=a0b353e9e5ebd34d83a672ffee5088e8

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1