MS Access:将一个 table 的多个查询结果与同一 table 的查询结果进行比较
MS Access: compare multiple query results from one table against the results of a query on the same table
我正在构建一个 MS 访问数据库来管理混合物的部件号。这几乎是一份材料清单。我有一个 table、tblMixtures
在 PreMixture
字段中引用自身。我设置它以便混合物可以是另一种混合物中的预混合物,而另一种混合物又可以是另一种混合物中的预混合物,等等。 tblMixture
中的每个 PartNumber
都与许多 Component
s 在 tblMixtureComponents
由 PartNumber
。 Component
及其相关数据存储在 tblComponentData
中。我在下面的 table 中输入了示例数据。
tblMixtures
PartNumber
Description
PreMixtures
1
Mixture 1
4, 5
2
Mixture 2
4, 6
3
Mixture 3
4
Mixture 4
3
5
Mixture 5
6
Mixture 6
tblMixtureComponents
ID
PartNumber
Component
Concentration
1
1
A
20%
2
1
B
40%
3
1
C
40%
4
2
A
40%
5
2
B
30%
6
2
D
30%
tblComponentData
ID
Name
Density
Category
1
A
1.5
O
2
B
2
F
3
C
2.5
I
4
D
1
F
我已经构建了将最终混合物的信息整合在一起所需的查询,甚至显示了用于每种混合物的预混合物和成分的详细信息。然而,对于数以万计的零件号,用于混合物的预混料可能会有很多重叠。换句话说,Mixture 4
可以用作 Mixture 1
和 Mixture 2
等的预混。我想建立一个查询来识别所有可能的混合物,这些混合物可以用作选定混合物中的预混合物。所以我想要一个列表,其中包含与所选混合物具有相同成分或成分子集的所有混合物。预混料不必包含混合物中的所有成分,但不能包含混合物中没有的任何成分。
如果你还没有解决...
存储数据集合的 PreMixtures 列表明您需要进一步“规范化”您的数据库设计。如果您要从查询中获取预混数据,则不需要将其存储为 table 数据。如果这样做,每次混合物或成分发生变化时,您都将被迫更新预混数据。
我们还需要解决 tblMixtures 没有 id 字段的问题。考虑以下 table 更改:
tbl混合物:
id
description
1
Mixture 1
2
Mixture 2
3
Mixture 3
tblMixtureComponent:
id
mixtureId
componentId
1
1
A
2
1
B
3
1
C
4
2
A
5
2
B
6
2
D
7
3
A
8
4
B
我个人喜欢使用公开主键与外键关系的列命名。 tblMixtures.id 显然与 tblMixtureComponenets.mixtureId 有关。我很懒,所以我也可能会缩写所有内容。
现在就查询而言,首先让我们获取混合物1的成分:
SELECT tblMixtureComponent.mixtureId, tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1
应该return:
mixtureId
componentId
1
A
1
B
1
C
我们可以将 WHERE 子句更改为我们想要的任何混合物的 ID。接下来我们需要获取所有含有不良成分的混合物 ID。所以我们将构建一个连接来比较最后一个查询:
SELECT tblMixtureComponent.mixtureId
FROM tblMixtureComponenet LEFT JOIN
(SELECT tblMixtureComponent.mixtureId,
tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1) AS GoodComp
ON tblMixtures.componentId = GoodComp.componentId
WHERE GoodComp.componentId Is Null
应该return:
mixtureId
2
太好了,现在我们有了不需要的所有混合物的 ID。让我们添加另一个连接以获得逆:
SELECT tblMixture.id
FROM tblMix LEFT JOIN
(SELECT tblMixtureComponent.mixtureId
FROM tblMixtureComponenet LEFT JOIN
(SELECT tblMixtureComponent.mixtureId,
tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1) AS GoodComp
ON tblMixtures.componentId = GoodComp.componentId
WHERE GoodComp.componentId Is Null) AS BadMix
ON tblMixtures.id = BadMix.mixtureId
WHERE BadMix.mixtureId = Null AND tblMixture.id <> 1
应该return:
mixtureId
3
4
剩下的是与混合物 1 具有相似成分但不相似成分的所有 id。
抱歉,我是在 phone...
上做的
我正在构建一个 MS 访问数据库来管理混合物的部件号。这几乎是一份材料清单。我有一个 table、tblMixtures
在 PreMixture
字段中引用自身。我设置它以便混合物可以是另一种混合物中的预混合物,而另一种混合物又可以是另一种混合物中的预混合物,等等。 tblMixture
中的每个 PartNumber
都与许多 Component
s 在 tblMixtureComponents
由 PartNumber
。 Component
及其相关数据存储在 tblComponentData
中。我在下面的 table 中输入了示例数据。
tblMixtures
PartNumber Description PreMixtures 1 Mixture 1 4, 5 2 Mixture 2 4, 6 3 Mixture 3 4 Mixture 4 3 5 Mixture 5 6 Mixture 6
tblMixtureComponents
ID PartNumber Component Concentration 1 1 A 20% 2 1 B 40% 3 1 C 40% 4 2 A 40% 5 2 B 30% 6 2 D 30%
tblComponentData
ID Name Density Category 1 A 1.5 O 2 B 2 F 3 C 2.5 I 4 D 1 F
我已经构建了将最终混合物的信息整合在一起所需的查询,甚至显示了用于每种混合物的预混合物和成分的详细信息。然而,对于数以万计的零件号,用于混合物的预混料可能会有很多重叠。换句话说,Mixture 4
可以用作 Mixture 1
和 Mixture 2
等的预混。我想建立一个查询来识别所有可能的混合物,这些混合物可以用作选定混合物中的预混合物。所以我想要一个列表,其中包含与所选混合物具有相同成分或成分子集的所有混合物。预混料不必包含混合物中的所有成分,但不能包含混合物中没有的任何成分。
如果你还没有解决...
存储数据集合的 PreMixtures 列表明您需要进一步“规范化”您的数据库设计。如果您要从查询中获取预混数据,则不需要将其存储为 table 数据。如果这样做,每次混合物或成分发生变化时,您都将被迫更新预混数据。
我们还需要解决 tblMixtures 没有 id 字段的问题。考虑以下 table 更改:
tbl混合物:
id | description |
---|---|
1 | Mixture 1 |
2 | Mixture 2 |
3 | Mixture 3 |
tblMixtureComponent:
id | mixtureId | componentId |
---|---|---|
1 | 1 | A |
2 | 1 | B |
3 | 1 | C |
4 | 2 | A |
5 | 2 | B |
6 | 2 | D |
7 | 3 | A |
8 | 4 | B |
我个人喜欢使用公开主键与外键关系的列命名。 tblMixtures.id 显然与 tblMixtureComponenets.mixtureId 有关。我很懒,所以我也可能会缩写所有内容。
现在就查询而言,首先让我们获取混合物1的成分:
SELECT tblMixtureComponent.mixtureId, tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1
应该return:
mixtureId | componentId |
---|---|
1 | A |
1 | B |
1 | C |
我们可以将 WHERE 子句更改为我们想要的任何混合物的 ID。接下来我们需要获取所有含有不良成分的混合物 ID。所以我们将构建一个连接来比较最后一个查询:
SELECT tblMixtureComponent.mixtureId
FROM tblMixtureComponenet LEFT JOIN
(SELECT tblMixtureComponent.mixtureId,
tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1) AS GoodComp
ON tblMixtures.componentId = GoodComp.componentId
WHERE GoodComp.componentId Is Null
应该return:
mixtureId |
---|
2 |
太好了,现在我们有了不需要的所有混合物的 ID。让我们添加另一个连接以获得逆:
SELECT tblMixture.id
FROM tblMix LEFT JOIN
(SELECT tblMixtureComponent.mixtureId
FROM tblMixtureComponenet LEFT JOIN
(SELECT tblMixtureComponent.mixtureId,
tblMixtureComponent.componentId
FROM tblMixtureComponent
WHERE tblMixtureComponent.mixtureId = 1) AS GoodComp
ON tblMixtures.componentId = GoodComp.componentId
WHERE GoodComp.componentId Is Null) AS BadMix
ON tblMixtures.id = BadMix.mixtureId
WHERE BadMix.mixtureId = Null AND tblMixture.id <> 1
应该return:
mixtureId |
---|
3 |
4 |
剩下的是与混合物 1 具有相似成分但不相似成分的所有 id。
抱歉,我是在 phone...
上做的