2012-02-21 34 views
2

我在Access數據庫中有三個表。在搜索查詢中有多個Ins位置?

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 

我希望我解釋清楚,讓我知道如果我需要澄清任何事情。

+1

訪問SQL使用'*'作爲通配符,不''%的數量。 – 2012-02-21 22:22:50

+0

這個問題的原始版本有asp-classic標籤。在這種情況下,他很可能使用ADO連接到Access數據庫文件...因此他必須**使用ANSI通配符(%和_而不是\ *和?)。 – HansUp 2012-02-21 23:20:01

+0

這是正確的漢斯,單一的查詢工作正常,它得到了第二個扔掉我。我會盡快給Tony的解決方案一個試試,如果我沒有收到我要發佈的一些數據。謝謝你! – 2012-02-22 15:32:47

回答

1

與上次查詢的問題是,你正在尋找一個LabStd排在那裏StdId在兩個子集,而這是不可能的。你想要的是找到兩個標準都存在的所有實驗室。 Someething這樣的:

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') 
    ) 
+0

賓果!萬分感謝! – 2012-02-22 19:23:15

1

像這樣的事情可能是,認爲standard.keywords認爲,這應該是一個像...

Select ID as StdID From Standards Where 
Standards.Keywords ='labstandard1' 
OR 
Standards.standard = "labstandard1" 
OR 
Standards.Keywords ='labstandard2' 
OR 
Standards.standard = "labstandard2" 

然後用所有拿到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") 

然後加入回到實驗室。

像這樣的查詢可以驅動你瘋了。始終將它們從內到外分解並確認零件。

+0

我喜歡這個查詢,因爲它比我想出的要乾淨得多,但我仍然得到相同的結果,我將得到一個具有其中一個標準但不是兩個標準的實驗室。 – 2012-02-22 15:47:50

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 

這是查詢的工作。這拉出拉出實驗ID10和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 
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