我写了一个查询,并从出生日期字段开始,计算出一个人的年龄,然后使用它AS age来创建年龄字段。
AS age
我的问题是,该年龄段是否可以再次比赛?
像这样
SELECT `candidates`.`candidate_id`, `candidates`.`first_name`, `candidates`.`surname`, `candidates`.`DOB`, `candidates`.`gender`, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) AS `age` FROM `candidates` WHERE `age` <= 20
任何帮助将不胜感激?
-----所以我将WHERE更改为HAVING,这是我的完整查询------
SELECT `candidates`.`candidate_id`, `candidates`.`first_name`, `candidates`.`surname`, `candidates`.`DOB`, `candidates`.`gender`, `candidates`.`talent`, `candidates`.`location`, `candidates`.`availability`, `candidate_assets`.`url`, `candidate_assets`.`asset_size` FROM `candidates` LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `C`.`candidate_id` WHERE `C`.`availability` = 'yes' AND C.talent = "actor" AND C.skill = "accents" AND C.gender = "male" AND HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <= 69 AND HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <=19 AND HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <=49
我收到以下错误,
您的SQL语法有误;在第15行的’HAVING DATE_FORMAT(NOW(),’%Y’)-DATE_FORMAT(candidates。DOB,’%Y’)-(DATE_’
candidates
DOB
对于我的一生,我不知道那是什么。
一种方法是将原始查询包装为子查询,然后将WHERE子句移至外部查询:
WHERE
SELECT * FROM ( SELECT `candidates`.`candidate_id`, -- this is the original query `candidates`.`first_name`, `candidates`.`surname`, `candidates`.`DOB`, `candidates`.`gender`, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) AS `age` FROM `candidates` ) as innertable WHERE `age` <= 20 -- this is now part of the outer query
澄清:此功能在MySQL 5中有效
注意:这假设原始查询有效