I have a three tables:
Checkbook table holds what I need to get
Receipts table holds a link to a owner table
Owner table knows what checkbook it is linked to
I need to get the checkbooks only when there are rows in the receipts that are linked to it (through the Owner table). I don’t know exactly how to do this, and it kinda seems circular. This is what I’ve tried:
SELECT chk.ID, chk.Description FROM tblCheckbook chk LEFT JOIN tblOwner o ON r.OwnerID = o.ID INNER JOIN tblCashReceipts r ON chk.ID = o.CheckbookID
But sql server complains that ‘The multi-part identifier ‘r.OwnerID’ could not be bound.’
What do I need to do to get this to work?
Each join has a
onclause that describes the relation. You just have to put the relations with the correct joins.There is no point in using a left join here as you are using an inner join in the second step. That only causes a larger set for the database to work with to get the same result.