Sql 根据Access中另一个查询的结果运行查询

Sql 根据Access中另一个查询的结果运行查询,sql,ms-access,ms-access-2016,Sql,Ms Access,Ms Access 2016,因此,我有一个Excel电子表格作为表格导入Access,我希望通过从中的搜索按不同条件搜索此表格。 来自的搜索需要使用AND函数搜索特定条件,但也需要在多个字段上执行文本搜索,作为OR函数,如关键字类型搜索 我可以使用两个不同的按钮分别运行这两个查询,但我不能同时运行它们,因为我将AND和OR搜索合并到一个查询中,整个查询作为OR运行,因此我不需要所需的结果 本质上,我想要的结果必须有X、Y和F,并且NADCAPNADCAP最多可以在6个字段中 我已经创建了一个union查询,但它作为OR查询

因此,我有一个Excel电子表格作为表格导入Access,我希望通过从中的搜索按不同条件搜索此表格。 来自的搜索需要使用AND函数搜索特定条件,但也需要在多个字段上执行文本搜索,作为OR函数,如关键字类型搜索

我可以使用两个不同的按钮分别运行这两个查询,但我不能同时运行它们,因为我将AND和OR搜索合并到一个查询中,整个查询作为OR运行,因此我不需要所需的结果

本质上,我想要的结果必须有X、Y和F,并且NADCAPNADCAP最多可以在6个字段中 我已经创建了一个union查询,但它作为OR查询而不是我所需要的

太好了。是否有一种方法可以让我先运行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] & "*"));
我的问题

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].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])));
另一种方法利用联合查询将字段重新排列为规范化结构-仍然需要唯一标识符字段

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 data]并应用筛选条件。请注意,这将导致数据集不可编辑。或引用此查询对象作为IN子句的子查询,如第一个示例所示。 和ID在查询中选择ID,如*&[Forms]![搜索]![认证]和*。结果可能仍然是不可编辑的数据集-我从未测试过


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

我同意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]))
)

您可能需要将标记ms access表单添加到此问题中。我建议您使用和SQL创建一个查询对象,并将其称为Query1。您可以从表单上的按钮运行该查询。然后使用OR SQL创建第二个查询对象,但将FROM子句更改为从Query1读取为[CP Data]。叫它Query2。您可以从表单上的第二个按钮执行Query2。这可能行得通,但我对表单的了解还不够,不知道如何在查询对象的where子句中嵌入表单控件值。谢谢!!我考虑过这种方法,但不确定如何编写查询。来自查询1的[CP数据]是我无法计算的位。我想这会起作用的,直到我能想出如何同时运行两者。您已经认识到的High 5'sAs应该规范化数据结构。与其有多个鉴定字段,不如有一个相关的从属表,其中每个鉴定都是一个记录。联合查询可以将字段重新排列为规范化结构。该查询可以连接到表进行搜索。这是我所知道的“同时运行两个”的唯一解决方法。可能是这样的,因为我刚刚测试了Nicholas的想法,第二个查询结果是空白的,因为它不应该感谢您上面的示例。我将看一看,但我的大脑确实对如何将不同的认证与我所在的公司联系起来感到困惑列表中,我将不得不使用各种其他标准来执行此操作。我理解主键背后的方法是将不同表中的数据链接在一起,但它会进一步混淆如何将这些认证链接到公司,因为它们有多个认证。我相信这是一个多对多的关系。这是一个垂直的学习曲线,能够创建一个工作数据库,但我将使用的数据不会很快改变,因此从另一个查询的结果运行一个查询以进一步筛选对我来说似乎是一个合理的选择,考虑到构建数据库所需的时间数据库从头开始。TBH我并不是真的想要一个数据库,更多的是使用access作为一种搜索表的方式,而不是使用Excel Ta