Unix Technical Forum

using the result of conditional statements in where

This is a discussion on using the result of conditional statements in where within the MySQL forums, part of the Database Server Software category; --> Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:03 AM
profke
 
Posts: n/a
Default using the result of conditional statements in where

Hi,

I'm trying to use the result of a conditional statement in a where
clause, but i'm getting 1)nowhere 2) desperate :-)

The query is simple:
--------
SELECT
idUser,
(@ageraw:=YEAR(CURRENT_DATE()) - YEAR(dateofbirth) -
(RIGHT(CURRENT_DATE(),5)<'12-31')) AS temp,
@age:=if( @ageraw > 100,0,
if( @ageraw < 10,0 ,
@ageraw
)) as age
from users u
---------

this works as a charm. But now I want to select users of a certain
age, and I add a where clause:
from users u having @age<50

This is not working. Nor is any other variant I can think of. How can
this be achieved?

greeting, and many thanks,

Paul

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:03 AM
Willem Bogaerts
 
Posts: n/a
Default Re: using the result of conditional statements in where

> @age:=if( @ageraw > 100,0,
> if( @ageraw < 10,0 ,
> @ageraw
> )) as age
>
> this works as a charm. But now I want to select users of a certain
> age, and I add a where clause:
> from users u having @age<50


As the fields is named "age", HAVING age<50 is what I would type. With
the @ character, you call the contents of the variable, not the contents
of a record field.

Best regards
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:03 AM
profke
 
Posts: n/a
Default Re: using the result of conditional statements in where

Hi Willem

> As the fields is named "age", HAVING age<50 is what I would type. With
> the @ character, you call the contents of the variable, not the contents
> of a record field.


Thanks for your reply. However, that does not work :-((
I get a syntaxerror....

any more idea's?

Paul

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:03 AM
profke
 
Posts: n/a
Default Re: using the result of conditional statements in where

On 31 jan, 09:53, "profke" <pwieg...@gmail.com> wrote:
> Hi Willem
>
> > As the fields is named "age", HAVING age<50 is what I would type. With
> > the @ character, you call the contents of the variable, not the contents
> > of a record field.

>
> Thanks for your reply. However, that does not work :-((
> I get a syntaxerror....


Well, not a syntax error.. i get a unknown field error...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:11 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com