我必须从中删除guide_category与guide表无关的行(无效关系)。
guide_category
guide
这是我想做的,但是它当然不起作用。
DELETE FROM guide_category AS pgc WHERE pgc.id_guide_category IN (SELECT id_guide_category FROM guide_category AS gc LEFT JOIN guide AS g ON g.id_guide = gc.id_guide WHERE g.title IS NULL)
错误:
您不能在FROM子句中指定目标表’guide_category’进行更新
由于锁定实现问题,MySQL不允许使用DELETE或引用受影响的表UPDATE。
MySQL
DELETE
UPDATE
您需要在JOIN此处制作一个:
JOIN
DELETE gc.* FROM guide_category AS gc LEFT JOIN guide AS g ON g.id_guide = gc.id_guide WHERE g.title IS NULL
或只是使用NOT IN:
NOT IN
DELETE FROM guide_category AS gc WHERE id_guide NOT IN ( SELECT id_guide FROM guide )