我在尝试从MySQL表中清除数据时遇到问题。问题是我们需要使用子查询中的表来标识需要删除的内容。
DELETE
FROM dates
WHERE event_ref IN (*list of event_refs*)
AND person_ref IN (
SELECT person.person_ref
FROM person
JOIN dates ON dates.person_ref = person.person_ref AND dates.event_ref = 14
WHERE dates.date < CURDATE()
)
Subquery
为所有人提供的日期早于今天。事件引用列表是我们要删除的事件列表,排除 event_ref = 14
将查询作为SELECT进行处理会返回我们要删除的所有内容,但是MySQL会在尝试使用删除时抱怨。
我真的不想花太多时间创建视图以引用上述查询,因为清理是一次性事件
答案 0 :(得分:0)
您可以使用子查询:
DELETE
FROM dates
WHERE event_ref IN (*list of event_refs*)
AND person_ref IN (SELECT person_ref FROM (
SELECT person.person_ref
FROM person
JOIN dates ON dates.person_ref = person.person_ref AND dates.event_ref = 14
WHERE dates.date < CURDATE()) sub
)
答案 1 :(得分:0)
您可以在同一表的子查询上使用内部联接
DELETE dates FROM dates
INNER JOIN (
SELECT person.person_ref
FROM person
JOIN dates ON dates.person_ref = person.person_ref AND dates.event_ref = 14
WHERE dates.date < CURDATE()) sub
) t on t.person_ref = dates. person_ref
AND event_ref IN (*list of event_refs*)