问题
I had good success (MSAccess - Query using a Table as Criteria) joining a Contacts table when Title matches a substring contained in an Include table using:
SELECT Contacts.Title
FROM Contacts
INNER JOIN Include ON Contacts.Title like '*' & Include.String & '*';
I think I now need to go further and Exclude some titles, using an Exclude table. Examples: The above approach properly grabbed the "CEO", but also grabbed the "CEO Admin Assistant", which I do not want.
The following is returning all rows; guessing because its using an .AND. not an .OR. comparing the entries in my Exclude list.
SELECT Contacts.Title
FROM Contacts
INNER JOIN Exclude ON Contacts.Title not like '*' & Exclude.String & '*';
Any suggestions how to accomplish the desired Exclusion using a table?
回答1:
Using an inner join
with not like
in the join criteria will yield a one-to-many relationship, since, for each row in Contacts, there are likely to be many records in Exclude which the Contacts record is not like and only one record that it is like.
Instead, if you wanted to continue using the calculated join criteria (as opposed to using not in
or not exists
with a subquery), I would suggest using a left join
on the Exclude table and an is null
condition within the where
clause to select only those records which don't have a matching record in Exclude, e.g.:
select contacts.title
from contacts left join exclude on contacts.title like '*' & exclude.string & '*'
where exclude.string is null
来源:https://stackoverflow.com/questions/54835343/msaccess-exclusion-join-w-wildcard