简体   繁体   中英

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 ? currently its giving null value for b.item_opening_balance which should give the actual item opening balance from master table.

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

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:

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. 2- Will get all data from 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. 2- Will get all data from inventory_master and NULL data as well.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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