如何在 where 子句中使用别名?
我试图在多列文本和备忘录中搜索我不想看到的某些短语和黑名单短语。
假设下表如下
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'm trying to search multiple columns of text and memos for certain phrases and blacklist phrases I don't want to see.
Assume the following table
stories:
id, title, author, publisher, content
Ex. I want to find all stories that mention (in any field) 'apples' but blacklist 'applesauce'.
SELECT stories.id, [stories.title] & " " & [stories.author] & " " & [stories.publisher] & " " & [stories.memo] AS allMyText
FROM stories
WHERE ((([allMyText]) Like "*apples*" And ([allMyText]) Not Like "*applesauce*"));
How do I use my alias in the where clause? I can't find any documentation on the subject:
1) Is this approach possible?
2) Wouldn't the alternative mean that I'd be performing multiple string concatenations on every row iteration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1.
这种方法可行吗?当然,把它放在子查询中。
2.
替代方案是否意味着我要在每一行迭代中执行多个字符串串联?是的,没错,替代方法是重复表达式。我不会让您厌倦这个替代方案的代码。
对于您的特定查询,您还可以使用此
1.
Is this approach possible?Sure, put it in a subquery.
2.
Wouldn't the alternative mean that I'd be performing multiple string concatenations on every row iteration?Yes that is right, the alternative is to repeat the expression. I won't bore you with the code for this alternative.
For your particular query, you can also use this
是的,Access/Jet/ACE“SQL”语言的文档严重缺乏,而且可用的少量文档都存在令人震惊的错误。
以下是一些有关 SQL 的一般文档:
“Joe Celko 的集合思维:SQL 中的辅助表、临时表和虚拟表”,第 12 章,第 235-237 页:
我认为这解释了为什么不能在 Access(Jet、ACE 等)的
WHERE
子句中使用as 子句
(“列别名”)。也就是说,请注意 Access 与 SQL 不兼容,因为它允许您在
SELECT
子句中以从左到右的方向使用as 子句
,例如,这是在 Access SQL 中合法(但在标准 SQL 中非法):Yes, the documentation for Access/Jet/ACE 'SQL' language is severely lacking and the little that is available has shocking errors.
Here's some documentation about SQL generally:
"Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL", ch12, pp235-237:
I think this explains why you cannot use an
as clause
("column alias") in theWHERE
clause in Access (Jet, ACE, whatever).That said, note that Access is non-compliant with SQL in that it allows you to use an
as clause
in theSELECT
clause in left-to-right direction e.g. this is legal in Access SQL (but illegal in Standard SQL):使用子查询:
Use Subqueries: