PDA

View Full Version : Left join involving 4 tables


carrera
05-02-2003, 07:17 AM
I've searched through tutorials regarding left join in sql statement. So far the example given involves only two tables.

"The LEFT JOIN returns all the rows from the first table (Employees), even if there are no matches in the second table (Orders). If there are rows in Employees that do not have matches in Orders, those rows also will be listed."

But what if I have 4 tables and if one of the table has no match, it will still display the rows.

My sql statement is:
Select tbItems.ItemCode, tbStockItem.LotNo, tbLocation.LocationCode, tbSampDetail.ContractNo from tbItems, tbStockItem, tbLocation, tbSampDetail where tbItems.ItemID=tbStockItem.ItemID and tbLocation.LocationID=tbStockItem.LocationID and tbStockItem.StockItemID=tbSampDetail.StockItemID

The above statement will return 0 rows if there are no matches found in tbSampDetail. What I need is to return all rows even if it does not have any matches in tbSampDetail.

allida77
05-02-2003, 02:31 PM
Try this:


Select
tbItems.ItemCode,
tbStockItem.LotNo,
tbLocation.LocationCode,
tbSampDetail.ContractNo
from
tbItems
INNER JOIN tbStockItem ON (tbItems.ItemID=tbStockItem.ItemID)
INNER JOIN tbLocation ON (tbLocation.LocationID=tbStockItem.LocationID)
LEFT OUTER JOIN tbSampDetail ON (tbStockItem.StockItemID=tbSampDetail.StockItemID)