SELECT
CASE WHEN c1.parent_id = 0 THEN c1.name ELSE ' - ' || c1.name END AS name
FROM category c1
LEFT JOIN category c2
ON c1.parent_id = c2.ID
ORDER BY
COALESCE(c2.ID, c1.ID),
c1.ID;
select
concat(case when parent_id <> 0 then '-' else '' end, name) name
from category
order by
case when parent_id = 0 then id else parent_id end,
parent_id