Sql 尝试获取订购次数最少的产品
我想找出订购次数最少的产品,而不使用order by desc或top 1等。我的查询似乎得到了正确的最小值计算,但productID和名称不匹配Sql 尝试获取订购次数最少的产品,sql,mariadb,left-join,aggregate-functions,having-clause,Sql,Mariadb,Left Join,Aggregate Functions,Having Clause,我想找出订购次数最少的产品,而不使用order by desc或top 1等。我的查询似乎得到了正确的最小值计算,但productID和名称不匹配 SELECT T.ProductID, ProductName, MIN(Total_Quantity) FROM (SELECT ProductID, SUM(Quantity) as Total_Quantity FROM orderdetails GROUP BY orderdetails.ProductID) as T
SELECT T.ProductID, ProductName, MIN(Total_Quantity)
FROM (SELECT ProductID, SUM(Quantity) as Total_Quantity
FROM orderdetails
GROUP BY orderdetails.ProductID) as T
JOIN products p on T.ProductID = p.ProductID
我的代码粘贴在上面,这是输出:
这是每个订购产品的数量总和
我想找出订购次数最少的产品
这听起来像是分组依据
和拥有
:
select p.productid, p.productname, sum(od.total_quantity) as total_quantity
from products p
left join orderdetails od on od.productid = p.productid
group by p.productid
having sum(od.total_quantity) > 3