问题
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?
回答1:
I can't use my alias in the where clause.
1.
Is this approach possible?
Sure, put it in a subquery.
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.
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
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*")
回答2:
The only problem is that, no matter what I try to do, I can't use my alias in the where clause. I can't find any documentation on the subject
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:
Here is how a
SELECT
works in SQL... Start in theFROM
clause... Go to theWHERE
clause... Go to the optionalGROUP BY
clause... Go to the optionalHAVING
clause... Go to theSELECT
clause and construct the expressions in the list. This means that the scalar subqueries, function calls and expressions in theSELECT
are done after all the other clauses are done. TheAS
operator can also give names to expressions in theSELECT list
. These new names come into existence all at once, but after theWHERE
clause,GROUP BY
clause andHAVING
clause have been executed; you cannot use them in theSELECT
list or theWHERE
clause for that reason.
I think this explains why you cannot use an as clause
("column alias") in the WHERE
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 the SELECT
clause in left-to-right direction e.g. this is legal in Access SQL (but illegal in Standard SQL):
SELECT 2 AS a, 2 AS b, a + b AS c
FROM tblMyTable
回答3:
Use Subqueries:
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*"))
来源:https://stackoverflow.com/questions/4893917/how-do-i-use-my-alias-in-the-where-clause