IF 语句中 MySQL SELECT 语法错误
我陷入了 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
这里有什么问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(不相关):您拥有的
IF
不是语句,而是函数。将
SELECT
子查询:放在括号之间:
(unrelated): The
IF
you have is not a statement, it's a function.Put the
SELECT
subquery:between parenthesis: