Wednesday, June 8, 2011

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.

No comments:

Post a Comment