我正在尝试运行以下查询:
SELECT ifnull(sum(item_actual_qty),0) + b.item_opening_balance as closing
FROM transaction_inventory AS a
LEFT JOIN inventory_master as b
ON b.item_code = a.item_code and b.company_code = a.company_code
WHERE a.item_Code = 2222
AND a.company_code = '52889497-5b6b-403d-8f83-224e3c7759b4'
AND a.trans_type_name NOT IN ('Sales Order','Purchase Order')
AND a.trans_date < '2010-04-01' ;
即使 transaction_inventory 中没有记录,我如何从 inventory_master 中选择记录?目前它为b.item_opening_balance提供空值,它应该提供主表中的实际项目期初余额。
放置一个子查询,例如
SELECT ifnull(sum(item_actual_qty),0) +
(select item_opening_balance from inventory_master where item_code = a.item_code) as closing
FROM transaction_inventory AS a
WHERE a.item_Code = 2222
AND a.company_code = '52889497-5b6b-403d-8f83-224e3c7759b4'
AND a.trans_type_name NOT IN ('Sales Order','Purchase Order')
AND a.trans_date < '2010-04-01'
从inventory_master返回项目期初余额,但我避免使用子查询