By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,744 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,744 IT Pros & Developers. It's quick & easy.

using conditional statements in where-clause

P: n/a
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

Jan 30 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a


On 30 Jan, 13:56, "pwieg...@gmail.com" <pwieg...@gmail.comwrote:
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
I would have expected it to be
from users u having age<50

Jan 30 '07 #2

P: n/a
>
I would have expected it to be
from users u having age<50
Me too.. but that does not work :-(
Any more idea's?

Jan 30 '07 #3

P: n/a
On Tue, 30 Jan 2007 12:56:13 -0800, pw******@gmail.com
<pw******@gmail.comwrote:
>>
I would have expected it to be
from users u having age<50

Me too.. but that does not work :-(
Any more idea's?
You'd probably get more responses from a SQL newsgroup. This doesn't seem
PHP-specific.

--
Curtis, http://dyersweb.com
Feb 1 '07 #4

P: n/a
"Captain Paralytic" <pa**********@yahoo.comwrote:
>

On 30 Jan, 13:56, "pwieg...@gmail.com" <pwieg...@gmail.comwrote:
>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

I would have expected it to be
from users u having age<50
Ummm, why isn't it "FROM users u WHERE age<50"? Do you have a GROUP BY
clause that you didn't show us? HAVING is only used with GROUP BY.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Feb 2 '07 #5

P: n/a
On 2 Feb, 06:40, Tim Roberts <t...@probo.comwrote:
"Captain Paralytic" <paul_laut...@yahoo.comwrote:
On 30 Jan, 13:56, "pwieg...@gmail.com" <pwieg...@gmail.comwrote:
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
I would have expected it to be
from users u having age<50

Ummm, why isn't it "FROM users u WHERE age<50"? Do you have a GROUP BY
clause that you didn't show us? HAVING is only used with GROUP BY.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.- Hide quoted text -

- Show quoted text -
Because you can't use column aliases in the WHERE clause but you can
in the HAVING clause.
>From the MySQL manual:
"A similar MySQL extension applies to the HAVING clause. The SQL
standard does not allow the HAVING clause to name any column that is
not found in the GROUP BY clause if it is not enclosed in an aggregate
function. MySQL allows the use of such columns to simplify
calculations."

Feb 2 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.