目标是更新Inventory表的object_category列,用于表中项目数量最少的对象(count(name))
我知道在为子句使用子查询时,子查询必须返回单个列或值。但是,我不确定如何在保持语法正确的同时实现我的目标。
这是我到目前为止的代码,子查询本身产生了正确的两个要更新的对象,而不是当我把它放在IN子句中时
UPDATE Inventory
SET object_category = 'ABC'
WHERE Inventory.object_category in (
select count(name) as nameCount , object_category
from Inventory
group by object_category
having nameCount <= min(nameCount));
答案 0 :(得分:0)
在MySQL中,您需要使用join
表达此逻辑。如果您只想更新一个类别,那么这就足够了
UPDATE Inventory i JOIN
(SELECT count(name) as nameCount, object_category
FROM Inventory
GROUP BY object_category
ORDER BY nameCount
LIMIT 1
) ii
ON ii.object_category = i.object_category
SET i.object_category = 'ABC';
您的子查询没有意义,也不起作用。