如何在 where 子句中使用我的别名?

Ray*_*erg 3 sql ms-access vba jet

我正在尝试在多列文本和备忘录中搜索我不想看到的某些短语和黑名单短语。

假设下表

stories: 
id, title, author, publisher, content
Run Code Online (Sandbox Code Playgroud)

前任。我想找到所有提到(在任何领域)“苹果”但将“苹果酱”列入黑名单的故事。

SELECT stories.id, [stories.title] & " " & [stories.author] & " " & [stories.publisher] & " " & [stories.memo] AS allMyText
FROM stories
WHERE ((([allMyText]) Like "*apples*" And ([allMyText]) Not Like "*applesauce*"));
Run Code Online (Sandbox Code Playgroud)

如何在 where 子句中使用我的别名?我找不到有关该主题的任何文档:

1)这种方法可行吗?
2)替代方案是否意味着我将在每行迭代中执行多个字符串连接?

Ric*_*iwi 5

我不能在 where 子句中使用我的别名。

1. 这种方法可行吗?

当然,把它放在子查询中。

SELECT *
FROM
(
SELECT stories.id, [stories.title] & " " & [stories.author] & " " & [stories.publisher] & " " & [stories.memo] AS allMyText
FROM stories
) AS SUBQ
WHERE ((([allMyText]) Like "*apples*" And ([allMyText]) Not Like "*applesauce*"));
Run Code Online (Sandbox Code Playgroud)

2. 替代方案是否意味着我将在每行迭代中执行多个字符串连接?

是的,没错,另一种方法是重复表达式。我不会让你厌烦这个替代方案的代码。

对于您的特定查询,您还可以使用此

SELECT stories.id, [stories.title] & " " & [stories.author] & " " & [stories.publisher] & " " & [stories.memo] AS allMyText
FROM stories
WHERE ([stories.title] Like "*apples*" OR [stories.author] Like "*apples*" 
  OR [stories.publisher] Like "*apples*" OR [stories.memo] Like "*apples*")
AND NOT ([stories.title] Like "*applesauce*" OR [stories.author] Like "*applesauce*"
  OR [stories.publisher] Like "*applesauce*" OR [stories.memo] Like "*applesauce*")
Run Code Online (Sandbox Code Playgroud)