简体   繁体   English

sql left join从left表中选择所有记录,即使rigth表中没有记录

[英]sql left join select all records from left table even if no records in rigth table

I am trying to run following query : 我正在尝试运行以下查询:

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

How do i select records from inventory_master even if there are not records in transaction_inventory ? 即使transaction_inventory中没有记录,如何从stocking_master中选择记录? currently its giving null value for b.item_opening_balance which should give the actual item opening balance from master table. 目前,它的b.item_opening_balance值为空,应提供主表中的实际物料期初余额。

Putting a sub query like 像这样放置子查询

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'

returns the item opening balance from inventory_master, but i am avoiding to use subquery 从ventory_master返回项目期初余额,但我避免使用子查询

You need to use an outer join if you want all records from one of the tables. 如果要从一个表中获取所有记录,则需要使用外部联接。

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

If you reorder your from clause, you can also use a left outer join: 如果对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' ;

Use left outer join if the left table is the one to have all records returned, regardless of null values in the right one, or right outer join if the table with all the records is on the right. 如果左表是返回所有记录的表,则使用左外部联接,而不管右表是否为空值;如果包含所有记录的表在右边,则使用右外部联接。

RIGHT JOIN 正确加入

1- Will get data (Matching with inventory_master) from transaction_inventory and NULL data as well. 1-还将从transaction_inventory和NULL数据中获取数据(与stocking_master匹配)。 2- Will get all data from 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' ;

* FULL OUTER JOIN as UNION OF RIGHT AND LEFT JOIN * * 完整的外部联接为左右联接的联合*

1- Will get all data from transaction_inventory and NULL data as well. 1-将同时从transaction_inventory和NULL数据中获取所有数据 2- Will get all data from inventory_master and NULL data as well. 2-还将从ventory_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' ;

as Full Join is not available , you can do full join using Right and Left Join Union, example is listed below. 由于无法使用完全联接,因此您可以使用左右联接并进行完全联接,示例如下。 please check 请检查

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM