How do I do this?
SELECT sum(buy-sale) as istock form tbl_product where istock > 0
I am not interested in the following methods.
SELECT sum(buy-sale) as istock form tbl_product where sum(buy-sale)> 0
P粉6210339282023-09-14 11:34:43
You just need to replace WHERE
with HAVING
;-)
SELECT SUM(`buy-sale`) AS `istock` FROM tbl_product HAVING `istock` > 0
But this will return the sum of all records. Usually you would do this when grouping by some criteria, such as by product ID or other criteria. For example:
-- 查找大订单 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;
This might help: https://www.mysqltutorial.org/mysql-having.aspx