Access DB中有三个表。
Labs (ID, TestLab)
Standards (ID, Standard, Keywords)
LabStd (ID, LabID, StdID)
实验室有很多标准。在今天之前我需要做的就是将搜索短语与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 Standards.Keywords LIKE "%labstandard%"
OR Standards.standard LIKE "%labstandard%"
ORDER BY Labs.id, Standards.ID
现在,如果在搜索中使用加号(labstandard1 + labstandard2),我需要拆分字符串,看看实验室是否符合搜索中的所有标准。我想我需要使用子查询并试图使用多个WHERE IN
语句,但这不起作用,所以我不知所措。
作为测试,我删除了LIKE
语句,然后转到了等号。即使每个查询都有匹配的标准,并且与两个标准相关联的实验室,以下查询也不会返回结果。
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
我希望我解释得那么清楚,如果我需要澄清任何事情,请告诉我。
答案 0 :(得分:1)
像这样的东西可能是,认为标准。关键词表明这应该是... ...
Select ID as StdID From Standards Where
Standards.Keywords ='labstandard1'
OR
Standards.standard = "labstandard1"
OR
Standards.Keywords ='labstandard2'
OR
Standards.standard = "labstandard2"
然后使用All获取labstds
select LabStd.LabID
Where StdId = all (Select ID as StdID From Standards Where
Standards.Keywords ='labstandard1'
OR
Standards.standard = "labstandard1"
OR
Standards.Keywords ='labstandard2'
OR
Standards.standard = "labstandard2")
然后加入实验室。
这样的查询可以驱使你疯狂。始终将它们从内到外分开并确认零件。
答案 1 :(得分:1)
您上次查询的问题在于您正在寻找单个LabStd行,其中StdId位于两个子集中,这是不可能的。你想要的是找到两个标准都存在的所有实验室。有点像这样:
SELECT
*
FROM
Labs
WHERE
EXISTS (
SELECT
1
FROM
LabStd INNER JOIN Standards ON LabStd.StdId = Standards.Id
WHERE
LabStd.LabId = Labs.Id
AND
( Standards.Standard = 'labstandard1' or Standards.Keywords = 'labstandard1' )
)
AND
EXISTS (
SELECT
1
FROM
LabStd INNER JOIN Standards ON LabStd.StdId = Standards.Id
WHERE
LabStd.LabId = Labs.Id
AND
( Standards.Standard = 'labstandard2' or Standards.Keywords = 'labstandard2' )
)
答案 2 :(得分:0)
我找到了一个解决方案虽然它很难看。首先是一些数据。
**Standards**
ID Standard Keywords
20 AAMA 505 Dry Shrinkage Thermal Cycling
26 AAMA 614 High Perf. Organic Coatings on Plastic
**Labs**
ID TestLab
10 TESTING, INC
21 LABORATORIES, INC.
3 FRESNO TESTING, INC.
**LabStd**
ID LabID StdID
283 3 20
284 10 20
285 21 20
291 21 26
299 10 26
这是有效的查询。拉出拉出实验室ID 10和21并省去3,这是正确的。
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 labs.testlab
FROM Standards INNER JOIN
(Labs INNER JOIN LabStd ON Labs.ID = LabStd.LabID)
ON Standards.ID = LabStd.StdID
WHERE standards.standard = "AAMA 505"
)
AND Labs.TestLab IN
(
SELECT labs.testlab
FROM Standards INNER JOIN
(Labs INNER JOIN LabStd ON Labs.ID = LabStd.LabID)
ON Standards.ID = LabStd.StdID
WHERE standards.standard = "AAMA 614"
)
ORDER BY Labs.id, Standards.ID
答案 3 :(得分:0)
另一种方法是使用派生表来计算匹配标准的数量。然后使用HAVING子句确保正确的匹配数。
注意:count(*) = 2
是唯一搜索字词的数量
SELECT Labs.ID, Labs.TestLab, Standards.Standard
FROM (
( Labs INNER JOIN LabStd ON Labs.ID = LabStd.LabID )
INNER JOIN Standards ON Standards.ID = LabStd.StdID )
INNER JOIN (
SELECT ls.LabID, COUNT(*) AS NumMatched
FROM Standards s INNER JOIN LabStd ls ON s.ID = ls.StdID
WHERE ( s.standard LIKE "%AAMA 505%" OR s.Keywords LIKE "%AAMA 505%")
OR ( s.standard LIKE "%AAMA 614%" OR s.Keywords LIKE "%AAMA 614%" )
GROUP BY ls.LabID
HAVING COUNT(*) = 2
) matched ON matched.LabID = Labs.ID