Access 2013 SQL:“不喜欢”问题

时间:2014-03-07 17:42:46

标签: sql ms-access ms-access-2013

我无法弄清楚为什么我的WHERE子句似乎仍然返回值为#34; SPEC" ...我在这里做错了什么? [H1姓氏]是唯一包含此指定/值的列。

SELECT [H1 LAST Name] & ", " & [H1 FIRST Name] AS [FULL Name],
       [H1 E-Mail] AS [E-Mail],
       IIF([H1 Cell Phone] IS NULL, [Home Phone], [H1 Cell Phone]) AS Phone
FROM   NameLookup
WHERE  ((NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%') OR (NameLookup.[H1 LAST Name] NOT LIKE '%MODEL%'))
  AND  (NameLookup.[H1 LAST Name] IS NOT NULL)

UNION ALL

SELECT [H2 LAST Name] & ", " & [H2 FIRST Name] AS [FULL Name],
       [H2 E-Mail] AS [E-Mail],
       IIF([H2 Cell Phone] IS NULL, [Home Phone], [H2 Cell Phone]) AS Phone
FROM   NameLookup
WHERE  ((NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%') OR (NameLookup.[H1 LAST Name] NOT LIKE '%MODEL%'))
  AND  (NameLookup.[H2 LAST Name] IS NOT NULL)

ORDER BY [FULL NAME]

2 个答案:

答案 0 :(得分:3)

您的查询接受记录

(NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%') OR (NameLookup.[H1 LAST Name] NOT LIKE '%MODEL%')

因此,如果姓氏不包含“MODEL”但包含“SPEC”,则会将其包含在您的结果中。

答案 1 :(得分:2)

LIKE的通配符不同,具体取决于运行查询的上下文。

从ADO开始,外卡是%_

NameLookup.[H1 LAST Name] NOT LIKE '%SPEC%'

从DAO开始,外卡是*?

NameLookup.[H1 LAST Name] NOT LIKE '*SPEC*'

或者您可以使用ALIKE而不是LIKE,然后数据库引擎将始终期望%_通配符,无论查询运行的上下文如何:

NameLookup.[H1 LAST Name] NOT ALIKE '%SPEC%'