gpt4 book ai didi

mysql - 根据父表中的条件更新表

转载 作者:行者123 更新时间:2023-11-29 10:51:15 25 4
gpt4 key购买 nike

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

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

关于mysql - 根据父表中的条件更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43688478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com