gpt4 book ai didi

mysql - 减去/求和自己的表格

转载 作者:可可西里 更新时间:2023-11-01 07:48:07 26 4
gpt4 key购买 nike

我有一张这样的 table :

id product_property_id product_id amount type
1 1 145 10 0
2 4 145 12 0
3 6 145 13 1
4 23 147 2 0
5 4 145 15 1
6 4 145 2 1

类型:

0:出局

1: 在

我想要什么:

例如:

product_id 145product_property_id 4 :

(15+2) - 12 = 5

product_id   product_property_id  new_amout
145 4 5

是否可以使用 SQL 获得此结果,或者我必须立即使用 php

最佳答案

如果我没理解错的话,这只是一个使用条件聚合的查询:

select product_id, product_property_id,
sum(case when type = 1 then amount
when type = 0 then - amount
else 0
end) as new_amount
from likethis
group by product_id, product_property_id;

关于mysql - 减去/求和自己的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063049/

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