我在数据库中定义了以下表格:
moderator (id, name)
parent_object (id, moderator_id, parent_name)
child_object (id, parent_id, child_name, quantity)
我想根据ID删除子对象。我目前有类似的东西有效:
delete from child_object where id = 3;
但是,存在安全漏洞,因为这允许任何主持人更新他们不拥有的任何子对象。如果moderator_id为2,如何删除此记录,以便只有在parent_object的moderator_id为2时才删除它?
答案 0 :(得分:1)
您可以使用INNER JOIN
DELETE c.* FROM child_object c
INNER JOIN parent_object p ON c.parent_id = p.id
WHERE c.id = 3 AND p.moderator_id = 2