mysql - Subtract colums from two tables -
there 2 tables need total available quantity
table t1 +-------------+------------+ | code | qty | +-------------+------------+ | | 500 | +-------------+------------+ table t2 +-------------+------------+ | code | qty | +-------------+------------+ | | 10 | +-------------+------------+ | | 20 | +-------------+------------+
with code result 970 instead of 470:
select `t1`.`code`, (ifnull(sum(`t1`.`qty`),0) - ifnull(sum(`t2`.`qty`),0)) totalqty `t1` left join `t2` on `t1`.`code` = `t2`.`code` group `t1`.`code`
what i'm doing wrong?
check code :)
select code,ifnull(sum(qty),0)-ifnull((select sum(qty) t2 t1.code = code),0) answer t1 group code
i edit answer incase want add condition base on code , prevent returning of null value try bro
Comments
Post a Comment