Home | Blog | Twitter @AndyJ | Contact Me | Snippets/Downloads | RSS

MySQL alias, variables and the WHERE clause

What a nice morning i had remembering that you cannot reference an ALIAS or a variable in a WHERE clause. So for anyone else that has a memory like mine (i'm jealous of fish put it that way)

standard SQL doesn't allow you to refer to a column alias in a where cause. Which make total sense (now) as the column value is most likely not determined at time of execution. So....

SELECT
@total := date_add(datefield, INTERVAL 1 YEAR) AS newDate
FROM
mytable
#WHERE
# @total > now() /*Invalid*/
#WHERE
# newDate > now() /*Invalid*/

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Marcio's Gravatar Great help for me. Thanks Andy for the question, and thanks Mike for the answer! ;)
# Posted By Marcio | 7/21/07 2:45 PM
Kenny Saunders's Gravatar Ya, Thanks Mike!
# Posted By Kenny Saunders | 2/17/08 9:02 PM
David's Gravatar Thanks for the tip... Now... How would you accomplish this?
# Posted By David | 4/14/08 11:30 AM
ill's Gravatar I was trying to figure out why my query wouldn't work. Thanks for posting this.
# Posted By ill | 4/25/08 5:49 PM
cheeto's Gravatar i think you meant...

#WHERE
# @total > now() /*valid*/
#WHERE
# newDate > now() /*Invalid*/

also what is the colon for? :=
SELECT
@total := date_add(dat.....
# Posted By cheeto | 5/20/08 9:02 AM
Richard Donkin's Gravatar cheeto wrote:
> i think you meant...
> #WHERE
> # @total > now() /*valid*/

No, I don't believe using a variable there is a truly valid solution. Variables change values when you least expect them to. You should only use variables as a way to store information *between* queries, not *within* a query, AFAIK.

A solution I've found elsewhere is that you can use the column alias in your HAVING clause. (http://dev.mysql.com/doc/refman/5.0/en/problems-wi...)
# Posted By Richard Donkin | 8/30/08 12:53 AM
MajiD Fatemian's Gravatar Hey,
You can use HAVING instead of WHERE in this case, it works perfectly fine and the difference is :

[quote]
The WHERE statement is executed to determine which rows should be included in the GROUP BY part, whereas HAVING is used to decide which rows from the result set should be used.
[/quote]

[refrence]
http://dev.mysql.com/doc/refman/5.0/en/problems-wi...
[/refrence]

Hope it helps,
MajiD
# Posted By MajiD Fatemian | 11/15/08 5:02 PM
Ennio Wolsink's Gravatar What MajiD said, I couldn't use variables nor aliases in the WHERE clause either, but the HAVING clause worked perfectly.
# Posted By Ennio Wolsink | 9/30/09 1:53 PM
Michael's Gravatar Yeah, this is what HAVING is for. You can use it without a GROUP BY clause as long as the alias you're using came from an aggregate function.

http://dev.mysql.com/doc/refman/5.0/en/problems-wi...
# Posted By Michael | 2/19/10 7:18 PM
BlogCFC / created by Raymond Camden / running version 5.9.5.003 / Contact AndyJarrett.com / Pet Rescue SOS www.redgiraffes.co.uk