I have a query,and I am having the following error returned however I cannot find the problem,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘HAVING DATE_FORMAT(NOW(), ‘%Y’) – DATE_FORMAT(
candidates.DOB, ‘%Y’) – (DATE_’ at line 14
this is my query,
SELECT `candidates`.`candidate_id`,
`candidates`.`first_name`,
`candidates`.`surname`,
`candidates`.`DOB`,
`candidates`.`gender`,
`candidates`.`talent`,
`candidates`.`location`,
`candidates`.`availability`,
DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d'))
as `age`,
`candidate_assets`.`url`,
`candidate_assets`.`asset_size`
FROM `candidates`
LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
WHERE `candidates`.`availability` = 'yes'
AND candidates.talent = "presenter"
AND candidates.gender = "male"
HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <= 39
AND HAVING DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(`candidates`.`DOB`, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(`candidates`.`DOB`, '00-%m-%d')) <=29
GROUP BY `candidates`.`candidate_id`
what is the problem, I am not an avid user of mySQL
Three things I see right away:
1 –
HAVINGshould only occur once in the query, not twice.2 –
HAVINGshould always come afterGROUP BY3 –
HAVINGonly applies to AGGREGATE FUNCTIONS, not something like a date comparison. If you want a date comparison you should use aWHEREclause.There may be more but your query is extremely hard to read in the current “format”. If you need more help, please put at least SOME effort into making your question readable.