匹配Access

时间:2019-01-24 20:36:11

标签: ms-access

我试图根据目标表中是否包含索引表中的字符串,编写一个访问查询以从索引表中检索数据:示例:

Index and Target files

INDEX
StationKey    StationID
Main          101
14            102
Broad         103

TARGET
StationName   Number
Main Street         
14th St            
Broad Ave        

我想从索引表中获取“主”,在目标表中查找包含它的字符串,并从索引表中附加StationID字段。

我已经根据另一篇StackOverflow帖子Trying to join Access tables with like statement with list in field在SQL中进行了尝试:

Select [TARGET].[StationName], [INDEX].StationID,[INDEX].StationKey
FROM [TARGET]
Where Instr( [TARGET].[StationName],[INDEX].StationKey)>0;

运行此命令仅要求我在StationID和StationKey上输入参数值。我对此很陌生,感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

据我所知,Target和Index是2个单独的表。因此,您需要其中一个foreign key来告诉db这2个表如何相互关联。

因此,例如,您可以将表更改为以下内容:

INDEX
StationKey    StationID
Main          101
14            102
Broad         103

TARGET
StationName   Number StationID
Main Street          101
14th St              102
Broad Ave            103

这样做将创建一个公共列,该列显示两个表之间的关系。

然后,您可以在查询的StationID列上使用join,并获得以下结果:

Joined Table on StationID
StationName   Number StationKey
Main Street          Main
14th St              14
Broad Ave            Broad

答案 1 :(得分:0)

我在Access中创建了表和数据。结果查询是

SELECT TARGET.StationName, INDEX.StationID, INDEX.StationKey
FROM TARGET, [INDEX]
WHERE (((InStr([TARGET].[StationName],[INDEX].[StationKey]))>0));

这产生了正确的结果。可以轻松地卸下(仔细地)多个括号,以方便阅读-Access将它们放在了那里。我的结果是:

StationName  StationID  StationKey
Main Street  101        Main
14th St      102        4
Broad Ave    103        Broad