gpt4 book ai didi

MYSQL where on sum(table1.column_name) < table2.other 列

转载 作者:行者123 更新时间:2023-11-29 11:45:48 26 4
gpt4 key购买 nike

我有两个表 products 和 sell_products

products -> id|name|quantity|price
sold_products -> id|product_id|quantity

我需要的是计算 sell_products 中的数量总和,并检查总和是否大于产品表中的数量。如下所示;

select p.* from products as p and sold_products as sp where p.quantity > sum(sp.quantity)

提前致谢;

最佳答案

您可以通过join 2个表并使用havinggroup by来实现此目的:

select p.quantity, sum(sp.quantity) 
from products as p
inner join sold_products as sp
on p.id = sp.product_id
group by p.id
having p.quantity < sum(sp.quantity)

关于MYSQL where on sum(table1.column_name) < table2.other 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34974383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com