WITH RECURSIVE result (
id,
GoodsTypeName,
parentid
) AS (
SELECT
id,
GoodsTypeName,
parentid
FROM
goodstype
WHERE
id = 2
UNION ALL
SELECT
i.id,
i.GoodsTypeName,
i.parentid
FROM
goodstype i
JOIN result ON i.parentid = result.id
) SELECT
*
FROM
result