mysql - Minus/Sum a table with itself -
i have 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 type :
0: out
1: in
what want :
for example :
product_id 145 product_property_id 4 :
(15+2) - 12 = 5
product_id product_property_id new_amout 145 4 5 is possible use sql result or have use php instant of ?
if understand correctly, query using conditional aggregation:
select product_id, product_property_id, sum(case when type = 1 amount when type = 0 - amount else 0 end) new_amount likethis group product_id, product_property_id;
Comments
Post a Comment