IF语句中的MySQL SELECT语法错误

时间:2012-01-16 15:45:35

标签: mysql select syntax if-statement syntax-error

我陷入了MySQL语法错误,我真的不知道错误在哪里。我正在执行一个相当庞大的SELECT语句,该语句由几个不同的select_expr子选择组成:

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`

导致错误的select_expr是介于中间的IF语句,整个语句的其余部分工作得非常好。错误输出是:

#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 

这里有什么问题?

1 个答案:

答案 0 :(得分:3)

(无关):你所拥有的IF不是一个陈述,而是一个功能。


放置SELECT子查询:

To_days( SELECT `enddatum` 
         FROM `position` 
         WHERE (`auftrag_id` = `a`.`id`) 
         ORDER BY `enddatum` DESC 
         LIMIT 1
       )
括号之间的

To_days( ( SELECT `enddatum` 
           FROM `position` 
           WHERE (`auftrag_id` = `a`.`id`) 
           ORDER BY `enddatum` DESC 
           LIMIT 1
         )
       )