0

我正在尝试运行以下查询:

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返回项目期初余额,但我避免使用子查询

4

2 回答 2

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' ;

如果左表是返回所有记录的表,则使用左外连接,无论右表中的空值如何,如果包含所有记录的表都在右侧,则使用右外连接。

于 2013-09-04T04:38:22.003 回答
0

正确加入

1- 也会从 transaction_inventory 和 NULL 数据中获取数据(与 inventory_master 匹配)。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' ;

由于完全连接不可用,您可以使用左右连接联合进行完全连接,示例如下所示。请检查

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
于 2013-09-04T04:42:05.393 回答