WITH RECURSIVE all_uploads (codigo, parent, ext, main) AS (
SELECT ut1.codigo, ut1.codigo_upload_temp_pai AS parent,
ut1.codigo_extensao AS ext, ut1.codigo AS main
FROM upload_temp ut1
WHERE ut1.codigo = 486
UNION ALL
SELECT ut2.codigo, ut2.codigo_upload_temp_pai AS parent,
ut2.codigo_extensao AS ext, au.main
FROM upload_temp ut2
JOIN all_uploads au ON au.parent = ut2.codigo
)
DELETE FROM upload_temp WHERE codigo IN (SELECT codigo FROM all_uploads);
最初の選択(with内)に開始点を配置するか、「ツリー」の上部を表す疑似列を作成する必要があります。これは、ツリー全体のすべての行で同じです。 with内の最初の選択に「topwhere」を配置する方が簡単な解決策です。