WITH RECURSIVE cte AS( SELECT a.* FROM tab a where a.pid='1' UNION ALL SELECT k.* FROM tab k INNER JOIN cte c ON c.id= k.pid )SELECT * FROM cte
pid:上级ID
ID:自身ID