View Single Post
Old 02-24-2015, 07:19 AM
JustinCase2 JustinCase2 is offline
Junior Contributor
Join Date: Apr 2006
Posts: 324
Default So..

If you're doing more queries than this you really should read up a bit on SQL and joining tables, it's not that complicated and highly useful.

Anyway, for this one it would be quite simple:

SELECT d.DocumentNo, o.OwningDepartment FROM documentsTbl d LEFT OUTER JOIN docOwnerTbl  o ON (d.Department1 = o.Department1) AND (d.Department2 = o.Department2)
What i do is, i give both the tables an alias (documentsTbl is d and docOwnerTbl is o). I don't really need to do that part, it's just for convenience. I could have written the full table name each time instead. Then, I join the tables, by specifying the 2 columns that need to mach (ON statement). LEFT OUTER JOIN meens that from the table on the left (the first one specified in the FROM clause), we want ALL records returned, even if we find no match in the second table. The value for the OwningDepartment Field will then be Null.
An INNER JOIN would have only returned the records that have matches (INNER joins are not LEFT nor RIGHT).

Last edited by JustinCase2; 02-24-2015 at 07:59 AM.
Reply With Quote