I have three tables in an Access DB.
Labs (ID, TestLab)
Standards (ID, Standard, Keywords)
LabStd (ID, LabID, StdID)
A lab has many standards. Prior to today all I needed to do was match a search phrase to the keyword or standard columns in the Standards table.
Select Labs.ID, Labs.TestLab, Standards.standard
FROM Standards INNER JOIN (Labs INNER JOIN LabStd
ON Labs.ID = LabStd.LabID)
ON Standards.ID = LabStd.StdID
WHERE Standards.Keywords LIKE "%labstandard%"
OR Standards.standard LIKE "%labstandard%"
ORDER BY Labs.id, Standards.ID
Now, if a plus sign is used in the search (labstandard1+labstandard2) I need to split the string and see if a lab matches all the standards in the search. I figure I need to use a subquery for this and tried to use multiple WHERE IN statements but that doesn’t work so I’m at a loss.
As a test I removed the LIKE statements and just went to equals. The following query returns no results even though there is a matching standard for each and a lab associated with both standards.
SELECT Labs.ID, Labs.TestLab, Standards.standard
FROM Standards INNER JOIN (Labs INNER JOIN LabStd
ON Labs.ID = LabStd.LabID)
ON Standards.ID = LabStd.StdID
WHERE LabStd.StdID IN
(SELECT ID AS StdID FROM Standards
WHERE (Standards.Keywords = 'labstandard1'
OR Standards.standard ='labstandard1')
)
AND LabStd.StdID IN
(SELECT ID as StdID
FROM Standards
WHERE (Standards.Keywords = 'labstandard2'
OR Standards.standard ='labstandard2')
)
ORDER BY Labs.id, Standards.ID
I hope I explained that clear enough, let me know if I need to clarify anything.
The problem with your last query is that you’re looking for a single LabStd row where StdId is in both subsets, and that isn’t possible. What you want is to find all the Labs for which both standards exist. Someething like this: