關於MySQL中刪除知足子查詢結果數據的操做

關於MySQL中刪除知足子查詢結果數據的操做

關鍵問題:MySQL不容許在子查詢中使用要刪除的表mysql

錯誤示範:

DELETE
FROM
post_activity_user
WHERE
id IN (
SELECT
    a.id
FROM
    post_activity_user a
LEFT JOIN post_activity_info b ON a.activity_id = b.id
WHERE
    b.enterprise_id = 10000000
AND b.activity_type = 3
)

正確處理:

DELETE
FROM
    post_activity_user
WHERE
    post_activity_user.id IN (
        SELECT
            *
        FROM
            (
                SELECT
                    a.id
                FROM
                    post_activity_user a
                LEFT JOIN post_activity_info b ON a.activity_id = b.id
                WHERE
                    b.enterprise_id = 10000000
                AND b.activity_type = 3
            ) ee
    )
相關文章
相關標籤/搜索