2011-02-04 79 views
2

我试图通过多列文本和备注来搜索某些短语,并列出了其他我不想看到的短语。假设如下表(请跳过约3NF任何笑话,这是一个减少。除非你有关于3NF *一个很好的笑话)Ms-Access:连接,创建别名,使用别名

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)不会意味着我会在每个行迭代上执行多个字符串连接吗?

任何文档链接将非常有帮助。

(*请问“键,整个键,并没有什么,但关键帮帮我吧科德”算不算一个玩笑?意见欢迎。

**为什么?因为我不想被提醒不要在苹果酱制作过程中每天都会发生苹果种族灭绝事件。)

回答

4

我不能在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*")); 

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

子查询方法真的不会有帮助,在我看来 - 它是过度设计解决方案的问题。顺便说一句,如果人们只是使用Access查询构建器来处理这种事情,他们会避免这种问题(它会产生正确的Jet/ACE兼容的SQL)。 – 2011-02-06 00:20:09

+0

@ David-W-Fenton:OP已经接受了这个答案,所以现在回过头来定义你的意思是“真的不会有帮助”。另外,我想看看你的“解决方案”,以便我们可以评估它的“过度工程”性。 – onedaywhen 2011-02-07 09:39:24

1

使用子查询:

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

唯一的问题是,无论 我尝试做,我可以在where子句中不使用我的别名 。我不能在主题

是的,对于访问/喷气/ ACE“SQL”语言文档严重缺乏,这是可用的具有震撼力的错误很少发现任何 文档。

下面是一些关于SQL一般文档:

“乔·塞科在集思想:辅助,时间,和SQL虚拟表”,CH12,pp235-237:

这里是一个多么SELECT在SQL作品... 启动FROM子句中......去 的WHERE条款...转到 可选GROUP BY条款...转到 可选HAVING条款...转到 SELECT子句并在列表中构造 表达式。这意味着 标量子查询,函数 调用和SELECT 中的表达式在所有其他子句 完成后完成。 AS运营商也可以 给 SELECT list中的表达式命名。这些新的名字来 到生活,突然间,但 后WHERE条款,GROUP BY条款 和HAVING条款已经 执行;由于这个原因,您不能在 SELECT列表或WHERE子句 中使用它们。

我想这就是为什么你不能访问WHERE子句中使用的as clause(“列别名”)(喷气机,ACE,等等)。

也就是说,请注意Access与SQL不兼容,因为它允许您以从左到右的方向在SELECT子句中使用as clause,例如,这是在Access SQL法律(但不合法的标准SQL):

SELECT 2 AS a, 2 AS b, a + b AS c 
    FROM tblMyTable 

...由此证明访问SQL是不是一个真正的SQL可言!