运行基于 Access 中另一个查询结果的查询

问题描述

所以我有一个 Excel 电子表格作为表格导入到 Access 中,我想通过搜索来自不同的条件来搜索这个表格。 搜索 from 需要使用 AND 函数搜索特定条件,但也需要对多个字段执行文本搜索作为 OR 函数(如关键字类型搜索

我可以使用 2 个不同的按钮分别运行这 2 个查询,但我不能同时运行它们,因为我在一个查询中组合了 AND 和 OR 搜索,整个查询作为 OR 运行,所以我没有我想要的结果。

本质上我想要的结果必须有 X 和 Y 和 F 和“NADCAP”(NADCAP 最多可以在 6 个字段中) 我创建了一个联合查询,它可以作为 OR 查询而不是我需要的查询....

太好了。有没有办法让我先运行 AND 查询,然后使用它的结果进行关键字搜索并找到符合所有条件的所有记录?

我认为可以通过使用子查询或派生表来完成吗?还是使用第一个查询结果作为 FROM 或 SELECT 字段?

我知道正确和最好的方法是使用正确的表等构建数据库,但我希望可以通过查询解决问题,因为我在构建数据库方面的经验为零......只是从当我陷入困境时,我在这样的论坛上读到了什么!!

我的 AND 查询

SELECT [CP data].[Company name],[CP data].[UK head office address],[CP data].[Other UK addessses],[CP data].[UK manufacturing operations],[CP data].[Company URL],[CP data].[Oil and Gas],[CP data].Renewables,[CP data].Aerospace,[CP data].[Medical & Pharmaceuticals],[CP data].Automotive,[CP data].Rail,[CP data].Chemical,[CP data].Accreditations1,[CP data].Accreditations2,[CP data].Accreditations3,[CP data].Accreditations4,[CP data].Accreditations5,[CP data].Accreditations6,[CP data].[Full Assembly],[CP data].Component,[CP data].omr
FROM [CP data]
WHERE ((([CP data].[Full Assembly]) Like "*" & [Forms]![Search]![Full Assembly] & "*") AND (([CP data].Component) Like "*" & [Forms]![Search]![Component] & "*") AND (([CP data].omr) Like "*" & [Forms]![Search]![omr] & "*"));

我的 OR 查询

SELECT [CP data].[Company name],[CP data].omr
FROM [CP data]
WHERE ((([CP data].Accreditations1)=[Forms]![Search]![Accreditation])) OR ((([CP data].Accreditations2)=[Forms]![Search]![Accreditation])) OR ((([CP data].Accreditations3)=[Forms]![Search]![Accreditation])) OR ((([CP data].Accreditations4)=[Forms]![Search]![Accreditation])) OR ((([CP data].Accreditations5)=[Forms]![Search]![Accreditation])) OR ((([CP data].Accreditations6)=[Forms]![Search]![Accreditation]));

谢谢

解决方法

最佳解决方案是规范化数据结构。如果你不这样做,你将继续面临这样的恶化。

假设有一个唯一标识符字段(或复合标识符),请考虑:

SELECT * FROM [CP data]
WHERE ((([CP data].[Full Assembly]) Like "*" & [Forms]![Search]![Full Assembly] & "*") 
AND (([CP data].Component) Like "*" & [Forms]![Search]![Component] & "*") 
AND (([CP data].OMR) Like "*" & [Forms]![Search]![OMR] & "*")) 
AND ID IN (SELECT ID FROM [CP data]
           WHERE ((([CP data].Accreditations1)=[Forms]![Search]![Accreditation])) 
           OR ((([CP data].Accreditations2)=[Forms]![Search]![Accreditation])) 
           OR ((([CP data].Accreditations3)=[Forms]![Search]![Accreditation])) 
           OR ((([CP data].Accreditations4)=[Forms]![Search]![Accreditation])) 
           OR ((([CP data].Accreditations5)=[Forms]![Search]![Accreditation])) 
           OR ((([CP data].Accreditations6)=[Forms]![Search]![Accreditation])));

另一种方法利用 UNION 查询将字段重新排列为规范化结构 - 仍然需要唯一标识符字段。

SELECT ID,Accreditations1 AS Accreditation,1 AS Src FROM [CP data]
UNION SELECT ID,Accreditations2,2 FROM [CP data]
UNION SELECT ID,Accreditations3,3 FROM [CP data]
UNION SELECT ID,Accreditations4,4 FROM [CP data]
UNION SELECT ID,Accreditations5,5 FROM [CP data]
UNION SELECT ID,Accreditations6,6 FROM [CP data];

将该查询加入到 [CP 数据] 并应用过滤条件。请注意,这将导致不可编辑的数据集。或者将此查询对象引用为 IN() 子句的子查询,如第一个示例所示。
AND ID IN (SELECT ID FROM QueryUNION WHERE Accreditation LIKE "*" & [Forms]![Search]![Accreditation] & "*")。结果可能仍然是不可编辑的数据集 - 我从未测试过。

强烈建议不要在命名约定中使用空格或标点符号/特殊字符。

,

我同意 June 的观点,标准化数据库结构是迄今为止更好的答案。但是,如果您只是在寻找同时包含上述 AND 查询和 OR 查询的选择条件的单个查询,则可以在此处找到。

SELECT      [CP data].[Company name],[CP data].[UK head office address],[CP data].[Other UK addessses],[CP data].[UK manufacturing operations],[CP data].[Company URL],[CP data].[Oil and Gas],[CP data].Renewables,[CP data].Aerospace,[CP data].[Medical & Pharmaceuticals],[CP data].Automotive,[CP data].Rail,[CP data].Chemical,[CP data].Accreditations1,[CP data].Accreditations2,[CP data].Accreditations3,[CP data].Accreditations4,[CP data].Accreditations5,[CP data].Accreditations6,[CP data].[Full Assembly],[CP data].Component,[CP data].OMR
FROM        [CP data]
WHERE       ((([CP data].[Full Assembly]) Like "*" & [Forms]![Search]![Full Assembly] & "*") 
AND         (([CP data].Component) Like "*" & [Forms]![Search]![Component] & "*") 
AND         (([CP data].OMR) Like "*" & [Forms]![Search]![OMR] & "*"));
AND         (
                ((([CP data].Accreditations1)=[Forms]![Search]![Accreditation])) 
    OR          ((([CP data].Accreditations2)=[Forms]![Search]![Accreditation])) 
    OR          ((([CP data].Accreditations3)=[Forms]![Search]![Accreditation])) 
    OR          ((([CP data].Accreditations4)=[Forms]![Search]![Accreditation])) 
    OR          ((([CP data].Accreditations5)=[Forms]![Search]![Accreditation])) 
    OR          ((([CP data].Accreditations6)=[Forms]![Search]![Accreditation]))
)