我希望这是一个简单的,我只是找不到如何得到的结果我想,也许我正在使用SQL错误的关键字?
我在寻找其中包含一个完整的名称字段的雇员表,本场可“萨姆”或“埃文斯先生”或“先生山姆·埃文斯”
我试图找到它包含一个名称字段名称的另一个表中的部分匹配 - 这通常是一个简短的名称,如“萨姆”或“埃文斯”,但可能是“山姆·埃文斯”
我试图像,有和没有*如下
SELECT tblEmployee.FullName, tblNames.Name
FROM tblEmployee, tblNames
WHERE tblEmployee.FullName Like "*" & tblNames.Name & "*"
以及结果我确实想,这种查询可以返回“参孙先生”,我不想要的员工全名。 我只希望看到的结果contaning完整的单词“萨姆”不FullNames与山姆在字符串中单词的一部分。 所以我想看看萨姆埃文斯先生和萨姆·史密斯先生和萨莉·埃文斯女士等。
我希望这是有道理的。 非常感谢您的帮助
编辑 - 解决方案
我已经解决了这一点 - 以防万一发布它可能会帮助任何有类似的问题。
WHERE " " & tblEmployee.FullName & " " Like "* " & tblNames.Name & " *"
所以基本上通过增加在开始和一个空间的雇员全名的结尾,我捕捉到所有正确的记录。 我没有意识到你可以垫了实地这样,但一个同事放倒我了!
Wildcards in SQL and access are a %
symbol;
so
"SELECT tblEmployee.FullName, tblNames.Name FROM tblEmployee, tblNames
WHERE tblEmployee.FullName Like '%" & tblNames.Name & "%'"
Will match any instance of the string tblNames.Name
You can use the %
at the start or end only to get the match of just the start of the string / end of the string repectively.
edit
Apologies, you can use *
and ?
in access as you expect, however are you sending the query as a string to some VBA code? Or trying to run it directly? As directly in a query it'll not be able to parse the tblNames.Name
string into the query and you'll need to pass that in from a form or other peice of code.
Edit based on comment
To select a specific word it's just a bit of a work around required:
SELECT * FROM table WHERE field LIKE '* myWord *' OR field LIKE '* myWord.*'
You can optionally caputure start of sentances `LIKE 'MyWord *'
and word with commas, fullstops, exclamtion marks etc...
You could do an IN statement and have all the variations in a lookup table to keep it easy to maintain as another option.
我用从后期编辑该解决方案在Access 2007中,它完美地工作!
WHERE " " & tblEmployee.FullName & " " Like "* " & tblNames.Name & " *"
现在的问题是,究竟发生了什么SQL代码做什么,为什么它的工作原理? 有人可以打破它?
编辑:
OK,我的困惑在访问SQL不同使用字符VS更标准的SQL出现。 &是串接在Access中,而+是concatnate标准SQL。 *是在访问任何数量的字符的通配符,而它是%的SQL。