IF 语句中 MySQL SELECT 语法错误

发布于 2024-12-27 09:55:23 字数 810 浏览 0 评论 0原文

我陷入了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2025-01-03 09:55:24

(不相关):您拥有的 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
         )
       )

(unrelated): The IF you have is not a statement, it's a function.


Put the SELECT subquery:

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

between parenthesis:

To_days( ( SELECT `enddatum` 
           FROM `position` 
           WHERE (`auftrag_id` = `a`.`id`) 
           ORDER BY `enddatum` DESC 
           LIMIT 1
         )
       )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文