终于看明白了,最好是在程序中实现
如果只有这两级,用SQL也可以,如果是无限分级的话,SQL是无能为力的
单就这个两级的来说:
SELECT tb.idname, tb_1.idname AS Expr1, tb_2.idname AS Expr2
FROM tb tb_2 RIGHT OUTER JOIN
tb tb_1 ON tb_2.parentid = tb_1.id RIGHT OUTER JOIN
tb ON tb_1.parentid = tb.id
WHERE (tb.parentid = 0)