MySQL Forums
Forum List  »  Newbie

Re: field values via formulas
Posted by: Felix Geerinckx
Date: June 01, 2005 07:34AM

Andriy Pasichnyk wrote:

> if i say:
>
> SELECT * FROM people WHERE age='22'
>
> than i'll select andrew and bob only, cuz it's 2005. but in 2006 cynth will turn 22 plus andy and
> bob will be 23.

Well don't use the age column then, but write this instead:

SELECT * FROM people WHERE YEAR(CURDATE()) - YearOfBirth = 22;

This will always work (except for the fact you only have the year and not the full date, as I noted in my first reply.


> so i was curious whether the 'age' value can automaticly be assigned a formula
> "YEAR(CURDATE()) - YearOfBirth" got it?

No, it cannot. The correct way to use computed values is by explicitely using the formula in your queries.


Another way would be to run the following query every 1st January:

UPDATE people SET age = YEAR(CURDATE()) - YearOfBirth;

But personally I prefer the first method.

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject
Written By
Posted
Re: field values via formulas
June 01, 2005 07:34AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.