通过以下查询,我可以找到要删除的数据:
SELECT ab.id, ab.anlage_id, a.id
FROM `anlagetobedienung` AS ab
LEFT JOIN anlage_new AS a ON ab.`anlage_id` = a.id
WHERE a.id IS NULL
如何编写删除anlagetobedienung表中找到的记录的DELETE语句?
答案 0 :(得分:4)
DELETE ab
FROM `anlagetobedienung` AS ab
LEFT JOIN anlage_new AS a ON ab.`anlage_id` = a.id
WHERE a.id IS NULL
答案 1 :(得分:0)
通常我会说:
delete anlage_new
where id in (
SELECT a.id
FROM `anlagetobedienung` AS ab
LEFT JOIN anlage_new AS a ON ab.`anlage_id` = a.id
WHERE a.id IS NULL
)
即。删除标识要删除的记录的select结果中主键所在的行。
但是根据你的查询,那些id = NULL
似乎没有任何意义。
您能提供一些示例数据吗?
编辑完成后,我会说这应该有效:
delete `anlagetobedienung`
where id in (
SELECT ab.id
FROM `anlagetobedienung` AS ab
LEFT JOIN anlage_new AS a ON ab.`anlage_id` = a.id
WHERE a.id IS NULL
)
注意:内部选择具有'ab.id'而不是'a.id',它似乎是原始选择中唯一可能包含有用内容的列。
答案 2 :(得分:0)
DELETE FROM anlagebedienung aDEL WHERE aDEL.id IN
(SELECT ab.id
FROM `anlagetobedienung` AS ab
LEFT JOIN anlage_new AS a ON ab.`anlage_id` = a.id
WHERE a.id IS NULL )
答案 3 :(得分:0)
另一种可能的解决方案:
DELETE FROM anlagetobedienung A
WHERE
NOT EXISTS (SELECT * FROM anlage_new AN WHERE AN.id = A.anlage_id)
如果MySQL支持DELETE FROM
中的别名(在MS SQL中支持,但我知道某些主要的RDBMS不支持这个),那么你可以这样做:
DELETE ab
FROM
anlagetobedienung AS ab
LEFT JOIN anlage_new AS a ON
ab.anlage_id = a.id
WHERE
a.id IS NULL
答案 4 :(得分:0)
你可以这样做:
DELETE FROM anlagetobedienung ab
WHERE NOT EXISTS (SELECT NULL
FROM anlage_new a
WHERE a.id = ab.anlage_id
)