我正在为我的 SQL 查询类做作业,但我陷入了子查询。我需要按仓库列出现有数量少于 20 件的元素数量。我只需要显示拥有超过 1 件商品且库存数量少于 20 件的仓库。这是我所拥有的:
select warehouse, on_hand, description
from part
where on_hand < 20 and
( select count(description)
from part
group by warehouse
)
order by warehouse;
我需要帮助的是子查询部分(如果我需要的话,子查询就是我所困扰的)。我不一定想要直接的答案,但需要帮助,例如提示和方向。
最佳答案
好吧,由于您不希望有人为您完成所有工作,并且您想使用子查询,因此您可以尝试以下操作:
select col1, total_items
from
( select col1,
sum(col2) as total_items
from part
group by col1
)
where total_items < 20 and total_items > 0
order by total_items;
请注意,您会将子查询视为“表”...
现在,使用 HAVING 子句:
select warehouse, count(description)
from part
where on_hand < 20
group by warehouse
having count(description) > 1
order by warehouse;
您可以了解有关此条款的更多信息here .
关于mysql - SQL子查询帮助(我相信使用聚合函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40094993/