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

phpmysqlcoderAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
ie

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.name, t.age, expected_income(t.age) as income
from  (select name, calculate_age(dob) as age from tbl ) t


0
 
_agx_Commented:
Not able how? If you're getting an error can you post the error message?
0
 
phpmysqlcoderAuthor Commented:
I get error "1054" unknown column "age" in the 'field list'.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
_agx_Commented:
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

0
 
_agx_Commented:
Out of curiosity what do these functions do? Is it something that can't be achieved with straight JOINs?
0
 
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.
0
 
_agx_Commented:
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.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.