Because age changes from year to year, you can do it.
Set the table as follows:
delimiter $$ CREATE TABLE `hello` ( `name` varchar(45) NOT NULL, `birthdate` date DEFAULT NULL, `job` varchar(45) DEFAULT NULL, `gender` enum('m','f') DEFAULT NULL, PRIMARY KEY (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8$$
The values I use are:
'A', '1980-08-04', 'clerk', 'm' 'B', '1969-10-12', 'waitress', 'f' 'C', '1992-09-16', 'pilot', 'm' 'd', '1991-02-21', 'unemployed', 'm'
SQL query:
select name,TIMESTAMPDIFF(YEAR,birthdate,current_date) as age,job,gender from hello where birthdate > current_date - interval 30 YEAR and birthdate < current_date - interval 20 year;
Query Responses Returned
name age job gender C 20 pilot m d 22 unemployed m
Added to SQLFiddle here. http://www.sqlfiddle.com/#!2/0143c/1/0
source share