Access 查询中的多个 AND
我的访问查询行为很奇怪,查询应该根据条件选择记录,但它不是这样做的。 查询是
select Distinct name from table1 where search like '%blue%' order by name
当我运行上述查询时,所有记录都返回包含蓝色关键字。
如果我向此查询添加更多单词,
select Distinct name from table1 where search like '%blue%' And '%red%' order by name
则应该选择那些蓝色和红色的记录。但它返回的记录只是蓝色或红色。它不适用“和”。 这是表结构
id path name search_keyword
1 c:\my picture\ red door red;
2 c:\my picture\ red door 38;
3 c:\my picture\ red door wood;
4 c:\my picture\ red door 2500;
5 c:\my picture\ red door smooth
6 c:\my picture\ blue door blue ;
7 c:\my picture\ blue door 38;
8 c:\my picture\ blue door wood;
9 c:\my picture\ blue door 2600;
19 c:\my picture\ blue door smooth;
My access query act so strange, the query is supposed to select the record according to the condtion but it is not doing like this.
the query is
select Distinct name from table1 where search like '%blue%' order by name
when i run the above query all the record are returned containing blue keyword.
if i add more word to this query,
select Distinct name from table1 where search like '%blue%' And '%red%' order by name
it is supposed to select those record which are blue and red. but it return the records which are just blue or red. it does not apply the and.
this is table structure
id path name search_keyword
1 c:\my picture\ red door red;
2 c:\my picture\ red door 38;
3 c:\my picture\ red door wood;
4 c:\my picture\ red door 2500;
5 c:\my picture\ red door smooth
6 c:\my picture\ blue door blue ;
7 c:\my picture\ blue door 38;
8 c:\my picture\ blue door wood;
9 c:\my picture\ blue door 2600;
19 c:\my picture\ blue door smooth;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意在 Access 会话中运行该查询时所使用的查询模式。您可以在此链接中找到有关 Access ANSI-89 和 ANSI-92 查询模式的全面讨论:Microsoft Access SQL 和 ANSI SQL 的比较
在另一个 Stack Overflow 问题中,您运行了类似的问题使用 OleDb 从 C# 查询来操作数据库。 OleDb 意味着您的查询将以 ANSI-92 模式运行,因此
%
是有效的通配符。但是,对于在 Access 会话中运行的查询,默认模式为 ANSI-89,这意味着相应的通配符为
*
而不是%
。如果您已通过“工具”->“选项”->“表/查询”选项卡配置了 Access,则为“此数据库”选择“SQL Server 兼容语法 (ANSI 92)” ,您的查询将需要 ANSI-92 通配符。
另一种选择是使用
Alike
而不是Like
比较运算符。Alike
向数据库引擎发出信号,要求使用 ANSI-92 通配符,无论查询在何处以及如何运行。这样,您就可以在 Access 会话中使用 ANSI-92 通配符,而无需设置我上面描述的选项。Be aware of the query mode you're using when you run that query in an Access session. You can find a thorough discussion of Access' ANSI-89 and ANSI-92 query modes at this link: Comparison of Microsoft Access SQL and ANSI SQL
In another of your Stack Overflow questions, you were running a similar query from c# using OleDb to operate with the database. OleDb means your query will run in ANSI-92 mode, so
%
is the valid wildcard character.However, for a query run in an Access session, the default mode is ANSI-89, which means the corresponding wildcard is
*
instead of%
.If you have configured Access by Tools->Options->Tables/Queries tab, then selected "SQL Server Compatible Syntax (ANSI 92)" for "This Database", your query will expect the ANSI-92 wildcards.
Another option is to use the
ALike
instead ofLike
comparison operator.ALike
signal the database engine to expect ANSI-92 wildcards regardless of where and how the query is run. That way you can use the ANSI-92 wildcard within an Access session without setting the option I described above.试试这个
从 table1 中选择不同的名称,其中搜索“%blue%”,并按名称顺序搜索“%red%”
try this
select Distinct name from table1 where search like '%blue%' And search like '%red%' order by name