CONTAINS 使用关键字 table

CONTAINS using a keyword table

我想创建一个查询,根据引用另一个 table 中的关键字字段的关键字查找 (CONTAINS) 来填充一个 table 中的字段。请参阅下面的示例 tables:

tbl_Parts
+----+------+-------------+------+
| ID | Comp |    Desc     | Type |
+----+------+-------------+------+
|  1 |  112 | Brg, Ball   |      |
|  2 |  245 | Bearing, X  |      |
|  3 |  364 | Mtg Ring, 1 |      |
|  4 |  445 | Pump, 2x3   |      |
+----+------+-------------+------+

tbl_Ref
+----+---------+-------+
| ID | Keyword | Type  |
+----+---------+-------+
|  1 | Bearing | O     |
|  2 | Ring    | S     |
|  3 | Pump    | P     |
|  4 | Disc    | O     |
+----+---------+-------+

具体来说,我想使用 tbl_Ref.Type WHERE tbl_Parts.Desc CONTAINS tbl_Ref.Keyword.

填充 tbl_Parts.Type

但是,我找不到任何使用字段作为参考的 CONTAIN 函数的示例。我想查询看起来类似于:

SELECT *
FROM (
SELECT tbl_Parts.Comp, tbl_Parts.Desc, tbl_Ref.Type AS tbl_Parts.Type 
FROM tbl_Ref, tbl_Parts 
WHERE tbl_Parts.Desc CONTAINS tbl_Ref.Keyword
) AS x;

我知道这远非正确,但目前我只能想到这些。

也会出现 tbl_Parts.Desc 包含多个关键字的情况。在这种情况下,我想连接所有类型匹配项。但是,目前此功能不是优先事项。

应该注意的是,我可以在 tbl_Parts 设计中的查找查询、独立查询或追加查询中执行此操作。

如果对此有任何建议或建议,那就太好了,在此先感谢!

CONTAINS 需要字符串文字作为第二个参数,所以我怀疑是否可行。

CONTAINS (
{
column_name | ( column_list )
| *
| PROPERTY ( { column_name }, 'property_name' )
}
,'<contains_search_condition>' [ , LANGUAGE language_term ]
)

您可以改用 LIKE

SELECT tbl_Parts.Comp, tbl_Parts.Desc, tbl_Ref.Type AS [tbl_Parts_Type] 
FROM tbl_Ref
JOIN tbl_Parts 
  ON tbl_Parts.Desc LIKE '%' +  tbl_Ref.Keyword + '%';

There will also be instances where tbl_Parts.Desc contains multiple keywords. In this case, I would like to concatenate all type matches. However, this functionality is not priority at the moment.

SELECT tbl_parts.ID, tbl_Parts.Comp, tbl_Parts.Desc,
  STRING_AGG(tbl_Ref.Type, ',') WITHIN GROUP(ORDER BY tbl_Ref.ID) AS tbl_Parts_Type 
FROM tbl_Ref
JOIN tbl_Parts 
  ON tbl_Parts.Desc LIKE '%' +  tbl_Ref.Keyword + '%'
GROUP BY tbl_parts.ID, tbl_Parts.Comp, tbl_Parts.Desc;

由于您最终打算使用从 SELECT 查询中获得的值填充 tbl_parts.type 字段(可能使用 UPDATE 查询),因此您可能需要使用DLookup 等域聚合函数来获取适当的值,否则 MS Access 会抱怨查询无法更新。

为此,我建议如下:

update tbl_parts p
set p.type = dlookup("type", "tbl_ref", "'" & p.desc & "' like '*' & keyword & '*'")

如果 tbl_parts.desc 包含引号,这当然会被破坏。

示例:

在 运行 以上 SQL 之后:

在描述匹配多个关键字的情况下,您需要将多个类型连接成一个逗号分隔的字符串,那么我建议构建一个 SELECT 查询,并根据 DLookup 表达式,然后使用 VBA.

遍历结果记录集