我该如何做到这一点?
SELECT sum(buy-sale) as istock form tbl_product where istock > 0
我对以下方法不感兴趣。
SELECT sum(buy-sale) as istock form tbl_product where sum(buy-sale)> 0
你只需要将WHERE替换为HAVING ;-)
WHERE
HAVING
SELECT SUM(`buy-sale`) AS `istock` FROM tbl_product HAVING `istock` > 0
但是这将返回所有记录的总和。通常你在按照某个条件进行分组时才会这样做,比如按产品ID或其他条件。例如:
-- 查找大订单 SELECT `order_number`, SUM(`quantity_ordered`) AS `quantity_sum`, SUM(`product_price` * `quantity_ordered`) AS `total` FROM order_details GROUP BY `order_number` HAVING `total` > 1000;
这可能会有所帮助:https://www.mysqltutorial.org/mysql-having.aspx
你只需要将
WHERE
替换为HAVING
;-)但是这将返回所有记录的总和。通常你在按照某个条件进行分组时才会这样做,比如按产品ID或其他条件。例如:
这可能会有所帮助:https://www.mysqltutorial.org/mysql-having.aspx