admin

Access 2013 SQL:``不喜欢''问题

sql

我无法弄清楚为什么我的WHERE子句似乎仍然返回值是“ 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]

阅读 145

收藏
2021-07-01

共1个答案

admin

LIKE根据查询运行的上下文,通配符会有所不同。

在ADO中,通配符为%_

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

在DAO中,通配符为*?

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

或者,您可以使用ALIKE代替LIKE,然后db引擎将始终期望%_通配符,而不管查询运行的上下文如何:

NameLookup.[H1 LAST Name] NOT ALIKE '%SPEC%'
2021-07-01