gpt4 book ai didi

mysql - mysql中的子查询定界符

转载 作者:行者123 更新时间:2023-11-30 22:06:42 25 4
gpt4 key购买 nike

我正在尝试更新名为 product_category_parent 的表的字段(例如 parent_id)。

这是我尝试使用的查询:

update sub_category_child set name='Mobile Phones', parent_id = (select parent_id from product_category_parent where cname = 'Appliances')

但它返回以下错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1

这是数据库模式

table product_category_parent
id cname
----- ----------
1 Electronics

table sub_category_child
id name parent_id
----- -------- -------------
1 Mobile Phone 1

parent_id 作为 product_category_parent.id 的外键

最佳答案

首先,您可能需要父表中的 id,而不是父表中的 parent_id,对吗?

UPDATE sub_category_child 
SET name = 'Mobile Phones',
parent_id = (SELECT id FROM product_category_parent WHERE cname = 'Appliances')

其次,通过此查询,您将更新 sub_category_child 表中的所有记录。这是您想要的,还是您缺少 WHERE 子句?

第三,如果子查询返回多于1条记录,你就有问题了。您不想选择使用哪一个进行更新(例如 ORDER by xx LIMIT 1),或者更可能的是 - 出了点问题。我假设 sub_category_child 中的每一行应该只有一个父级,那么为 Mobile Phones 类别找到父级的真实条件是什么?

关于mysql - mysql中的子查询定界符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41450121/

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