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

标签 sql ms-access vba jet

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

假设如下表

stories: 
id, title, author, publisher, content

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

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

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

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

最佳答案

I can't use my alias in the where clause.

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*"));

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*")

关于sql - 如何在 where 子句中使用我的别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893917/

相关文章:

excel - 使用 vba 修剪值时类型不匹配(错误 13)

excel - 如何根据列内容复制excel中的行?

SQL 计数正则表达式匹配 (PostgreSQL)

sql - 从 presto 中删除完全重复的行

vba - 如何仅在 VBA Access 中捕获 vbKeyReturn

mysql - 从代码循环到 Sql 查询?

java - 如何阻止数据库 Access .accdb 文件

sql - 这个数据库表应该规范化吗?

php - PDO SQL-state "00000"但仍然出错?

excel - 使用 vba 宏以编程方式将彩色垂直带添加到 Excel 图表