删除自关联表下的所有数据

发布时间:2024年01月01日
<update id="deleteConfig">
    WITH RECURSIVE DeleteHierarchy AS (
    SELECT id, parent_id
    FROM fsd_risk_analysis_config
    WHERE id IN
    <foreach collection="ids" open="(" close=")"  item="id" separator=",">
        #{id}
    </foreach>

    UNION ALL

    SELECT t.id, t.parent_id
    FROM fsd_risk_analysis_config t
    JOIN DeleteHierarchy d ON t.parent_id = d.id
    )
    update fsd_risk_analysis_config set is_deleted=1
    WHERE id IN (SELECT id FROM DeleteHierarchy);
</update>

WITH RECURSIVE DeleteHierarchy AS (
? SELECT id, parent_id
? FROM your_table_name
? WHERE id = 2

? UNION ALL

? SELECT t.id, t.parent_id
? FROM your_table_name t
? JOIN DeleteHierarchy d ON t.parent_id = d.id
)
DELETE?
FROM
?? ?your_table_name?
WHERE
?? ?id IN ( SELECT id FROM DeleteHierarchy );

WITH RECURSIVE DeleteHierarchy AS (
? SELECT id, parent_id
? FROM your_table_name
? WHERE id IN (1, 2, 3) -- 替换为你的ID列表

? UNION ALL

? SELECT t.id, t.parent_id
? FROM your_table_name t
? JOIN DeleteHierarchy d ON t.parent_id = d.id
)
DELETE FROM your_table_name
WHERE id IN (SELECT id FROM DeleteHierarchy);

文章来源:https://blog.csdn.net/tiantiantbtb/article/details/135330163
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。