What is needed here is to LEFT JOIN
the table against itself to get the category and the sub-category. The COALESCE()
returns either the concatenated value (which is NULL
if there's no matching sub-category) or the concatenated value.
SELECT
s.id,
COALESCE(CONCAT(c.name, '-', s.name), s.name) AS catname
FROM
cats s
LEFT JOIN cats c ON s.sub = c.id
ORDER BY catname ASC
Here is an example: http://sqlfiddle.com/#!2/201b1/8