Home  >  Q&A  >  body text

Translate "Operator value in where condition" into Chinese as "Operator value in where condition"

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粉754477325P粉754477325402 days ago627

reply all(1)I'll reply

  • P粉621033928

    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

    reply
    0
  • Cancelreply