I am stuck in a MySQL syntax error and I really have no idea where the mistake is. I am executing a rather huge SELECT statement consisting of several different select_expr subselects:
SELECT
`a`.`id` AS `id`,
`a`.`name` AS `name`,
/* ... */
IF(((SELECT COUNT(*) FROM `position` WHERE (`auftrag_id` = `a`.`id`) AND (`status` < 100)) = 0 ), ( To_days(SELECT `enddatum` FROM `position` WHERE (`auftrag_id` = `a`.`id`) ORDER BY `enddatum` DESC LIMIT 1) - To_days(`a`.`datum`) ), NULL) AS `days`,
/* ... */
FROM
`auftrag` `a`
The select_expr causing the error is the IF-statement inbetween, the rest of the whole statement works very well. The error output is:
#1064 - 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 'SELECT `enddatum` FROM `position` WHERE (`auftrag_id` = `a`.`id`) ORDER B' at line 50
What’s the problem here?
(unrelated): The
IFyou have is not a statement, it’s a function.Put the
SELECTsubquery:between parenthesis: