前提
我必须修改一个非常旧的 Access 2000 数据库。我有两张桌子:
产品表:
- pID as Text - 这是关键字段(是的,是文本,我对此无能为力)
- pLevel as Integer - 这是产品级别:0=已完成,1 到 7=子产品
- pDescription as text - 只是一个描述
P-关联表:
- pIDParent 作为文本
- pIDChild 作为文本
该表所做的是(子)产品与其他子产品之间的关联。“规则”是 pIDChild 级别必须高于 pIDParent 级别。
然后是一个表格,显示给定 pID 的所有孩子。这是基于查询的“链”:
这是查询 q-pAssociation0,它查看所选产品并返回子产品:
SELECT DISTINCTROW [t-Associations].pIDParent, [t-Associations].pIDChild, [t-Products].pLevel AS pParentLevel, [t-Products].pLevel AS pChildLevel FROM [t-Products] INNER JOIN [t-Associations] ON [t-Products].pID = [t-Associations].pIDChild
WHERE ((([t-Associations].pIDParent)=[Forms]![fTreeProdotti]![txtProdID]));
然后有 7 个查询(q-pAssociation1~q-pAssociation7),每个查询都处理前一个查询。这是 q-pAssociation1:
SELECT DISTINCTROW [q-pAssociation0].pIDChild AS pIDParent, [t-Associations].pIDChild, [q-pAssociation0].pChildLevel AS pParentLevel, [t-Products].pLevel AS pChildLevel FROM [q-pAssociation0] INNER JOIN ([t-Products] INNER JOIN [t-Associations] ON [t-Products].pID = [t-Associations].pIDChild) ON [q-pAssociation0].pIDChild = [t-Associations].pIDParent;
最后,有一个 GROUP 查询将所有以前的查询分组,并且表单是基于该查询。
问题 我必须修改这一切,以便查询仅返回具有 Level = parent level+1 的 Childs(因此,如果 Parent 有 Child 2 level“down”或更高级别,则不必返回。
所以,我在每个查询中添加了一个条件:
SELECT DISTINCTROW [q-pAssociation0].pIDChild AS pIDParent, [t-Associations].pIDChild, [q-pAssociation0].pChildLevel AS pParentLevel, [t-Products].pLevel AS pChildLevel
FROM [q-pAssociation0] INNER JOIN ([t-Products] INNER JOIN [t-Associations] ON [t-Products].pID = [t-Associations].pIDChild) ON [q-pAssociation0].pIDChild = [t-Associations].pIDParent
WHERE ((([t-Products].pLevel)=[q-pAssociation0]![pParentLevel]+1));
但是现在 q-pAssociation7 需要 10 分钟才能返回大约 15 条记录,而没有“WHERE”条件它几乎立即返回大约 25 条记录。
我怎样才能只获得仅下降一级的产品而没有这个缓慢的查询?