在查询结果中,可以增加level列,然后在最后的where语句中进行过滤。 WITH org AS ( SELECT id, name, pid, 1 AS level FROM t1 WHERE pid = 'A' UNION ALL SELECT t1.id, t1.name, t1.pid, t2.level + 1 AS level FROM tmp_t1 t1 INNER JOIN org t2 ON t1.pid = t2.id ) SELECT * FROM org where level <=2
在查询结果中,可以增加level列,然后在最后的where语句中进行过滤。