我正在尝试运行以下查询:
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返回项目期初余额,但我避免使用子查询
答案 0 :(得分:1)
如果您想要其中一个表中的所有记录,则需要使用外部联接。
select ifnull(sum(item_actual_qty),0) as aa,b.item_name,a.item_Code from transaction_inventory AS a right outer 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' ;
如果您重新排序from子句,还可以使用左外连接:
select ifnull(sum(item_actual_qty),0) as aa,b.item_name,a.item_Code from inventory_master as b left outer join transaction_inventory AS a
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' ;
如果左表是返回所有记录的表,而不管右表中的空值,则使用左外连接,如果具有所有记录的表位于右侧,则使用右外连接。
答案 1 :(得分:0)
正确加入
1-将从transaction_inventory获取数据(与inventory_master匹配)以及NULL数据。 2-来自inventory_master的获取所有数据。
SELECT ifnull(sum(item_actual_qty),0) as aa,b.item_name,a.item_Code
FROM transaction_inventory AS a
RIGHT 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' ;
* 作为右和左联合联盟的全部联接*
1-还会从transaction_inventory和NULL数据获取所有数据。 2-来自inventory_master的获取所有数据以及NULL数据。
SELECT ifnull(sum(item_actual_qty),0) as aa,b.item_name,a.item_Code
FROM transaction_inventory AS a
LEFT OUTER 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' ;
UNION
SELECT ifnull(sum(item_actual_qty),0) as aa,b.item_name,a.item_Code
FROM transaction_inventory AS a
RIGHT OUTER 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' ;
由于Full Join不可用,您可以使用Right和完全加入 左加盟,示例如下。请检查
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id