Translate "Operator value in where condition" into Chinese as "Operator value in where condition"
P粉754477325
P粉754477325 2023-09-13 13:58:40
0
1
605

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粉754477325
P粉754477325

reply all(1)
P粉621033928

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!