reusing calculated column (function)

I am trying to reuse a calculated field in mysql, the calculated field is a stored function. I am not able to reuse the output of the calculate_age function (age) as an input for the expected_income function as i get error "1054" unknown column "age" in the 'field list'.

Any Ideas?
select name, calculate_age(dob) as age, expected_income(age) as income from tbl;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Not able how? If you're getting an error can you post the error message?
phpmysqlcoderAuthor Commented:
I get error "1054" unknown column "age" in the 'field list'.
If you wanted to use the results of another function, I think you'd either have to nest them

          SELECT calculate_age(dob) as age, expected_income( calculate_age(dob) ), .....

... which would mean running the function twice OR use a derived table

        SELECT  expected_income( t.age ) ....
        FROM    ( SELECT  name, calculate_age(dob)  as age, .... FROM tableName )  t

The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

Out of curiosity what do these functions do? Is it something that can't be achieved with straight JOINs?
phpmysqlcoderAuthor Commented:
Actually I am just simplifying the problem. The actual 1st function has many arguments and I am looking for a way to have the calculated results from the 1st function as an input in the 2nd function.
Did you try the two options I mentioned above? ie Either nest the functions OR use a derived query so the result of function 1 is available as an actual column? Aside from reworking the functions, those are the only 2 methods I know of.


obviously the calculated value doesn't exist yet when running function2, because they're executed in parallel.  afaik your only choices are to either feed the results of function1 into function2

ie nesting...
select name, calculate_age(dob) as age, expected_income( calculate_age(dob) ) as income from tbl;

... OR used a derived table so that calculated value *does* exist as a column

derived table
select, t.age, expected_income(t.age) as income
from  (select name, calculate_age(dob) as age from tbl ) t


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.