I have field date_of_birth....
If I want to calculate age
"select floor((to_days(curdate())-to_days(date_of_birth))/5) as age
from table"
So I can use the above one...

instead of executivg this at every time I want,
Is it possible to create a field named age with above structure so If I store the date_of_birth, I can retrieve age automatically?

MySql does not support computed columns, but you can write an insert/update trigger to fill in the age column of your table.

you can create view where you can calculate age using above formula and other required columns,
Then you many use your view everywhere, now you just have to use column name age, no need to give formula again.

Can you explain me with an example please?

Run following sql queries in phpmyadmin sql window. This is how you can create view.

create view  mytableview  as select col1,col2,col3, col4 floor((to_days(curdate())-to_days(date_of_birth))/5) as age
from table

This is how you can use view, if you update date of birth view will give the latest value. no need recalucate or update.

select col1,col2,col3,age from mytableview
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.