将"Operator value in where condition"翻译成中文为"在where条件中的运算符值"
P粉754477325
P粉754477325 2023-09-13 13:58:40
0
1
684

我该如何做到这一点?

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

P粉754477325
P粉754477325

全部回复(1)
P粉621033928

你只需要将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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板