繁体   English   中英

根据父表中的条件更新表

[英]Update table based on condition in parent table

我在数据库中定义了以下表:

moderator (id, name)

parent_object (id, moderator_id, parent_name)

child_object (id, parent_id, child_name, quantity)

我想根据ID更新子对象。 我目前有这样的东西可以工作:

update child_object set child_name = "Fred", quantity=5 where id = 3; 

但是,存在一个安全漏洞,因为这允许任何主持人更新他们不拥有的任何子对象。 如果moderator_id为2,如何在此表上进行更新,以便仅在parent_object的moderator_id为2时更新?

在mysql update您可以进行联接。

update
  child_object c
  join parent_object p on p.id = c.parent_id
set
  c.child_name = "Fred",
  c.quantity = 5   -- Seriously, this better not be a string.
where
  c.id = 3 and p.moderator_id = 2
update child_object set child_name = "Fred", quantity="5" 
where id = 3 AND parent_id IN (Select id from parent_object where moderator_id = 2)

这应该可以,但是使用“ IN”很昂贵。 或者,您首先编写一个测试查询以验证是否允许更新。

Select parent_id, moderator_id FROM child_object c
Left Join parent_object p on p.id = c.parent_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM