Wednesday, June 8, 2011

Left Join - Another way

In the previous post, I used the LEFT JOIN (SELECT * from file) filename on filename.key=other.key to do a left join on tables not specified in the FROM Clause.

Here is another way to accomplish it. I don't know how it works but it does and I will dissect it until I understand it, maybe<G>.

SELECT batchcontract.certificate,
ClaimTotal.SumOfCheckAmount,
batchheader.uniquekey,
  Dealer.State
  AS DealerState
  FROM
(
(BatchHeader  LEFT JOIN Dealer ON  BatchHeader.Dealer = Dealer.uniquekey)
   LEFT JOIN  BatchContract ON BatchHeader.uniquekey =  BatchContract.BatchNum
)

LEFT JOIN ClaimTotal ON  BatchContract.uniquekey = ClaimTotal.iWarranty

WHERE iWarranty= 24492;

MySQL Left Join on table other than the one in the FROM clause

  I needed to do a left join on a link between two tables and neither was the one specified on the FROM clause. So I found this way to accomplish this task.

select
ac4.*,
checkheader.*,
archeckdetail.*,
artransactions.*,
arcredits.*
from
checkheader
left join archeckdetail on archeckdetail.icheck=checkheader.uniquekey
left join artransactions on artransactions.iCheck = checkheader.uniquekey
left join arcredits on arcredits.iCheck=checkheader.uniquekey
left join (Select * from arcredits  ) ac4 on  archeckdetail.icredit = ac4.uniquekey
where checkheader.uniquekey=1166

The ac4 table is specified in the left join which is linked to the arcredits table, already used and linked in the query.  This was an instance where the arcredit rows were used in different places for different purposes, thus necessitating the work around to get the second version into a query.

OK, after posting this, I also found that simply using:
left join arcredits ac4 on archeckdetail.icredit=ac4.uniquekey
works just as well.

It looks as though the previously defined Table in the LEFT JOIN is used for the LEFT Table.