将SELECT MAX嵌套在LEFT JOIN中时出现SQL错误#1064

时间:2015-01-01 23:45:46

标签: mysql phpmyadmin

请让我知道为什么我在以下SQL上遇到错误(我正在使用MySQL数据库)以及如何纠正它:

SELECT at_award_description.ad_id,
    at_award_description.ad_detail,
    at_award_description.ad_archived_date,
    a.cad_id,
    a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
    AND (a.ca_id = 37)
    AND a.cad_task_completion_date = (SELECT MAX(b.cad_task_completion_date)
                                      FROM at_cub_award_date AS b
                                      WHERE a.ca_id = b.ca_id AND a.ad_id = b.ad_id
                                     )
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;

我打破了SQL以确保每个部分都有效。

SELECT MAX(b.cad_task_completion_date)
FROM at_cub_award_date AS b

SELECT at_award_description.ad_id,
    at_award_description.ad_detail,
    at_award_description.ad_archived_date,
    a.cad_id,
    a.cad_task_completion_date
FROM at_award_description
LEFT JOIN at_cub_award_date AS a ON ((at_award_description.ad_id = a.ad_id)
    AND (a.ca_id = 37)
)
WHERE at_award_description.aw_id = 5
ORDER BY at_award_description.ad_order;

这些都是独立完成的。当我将它们组合时,我收到以下错误: 错误 SQL查询: 限制0,25 MySQL说:  #1064 - 您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在' LIMIT 0,25'附近使用。在第1行

此致

格林

EDIT @Barmar提出了删除&#34 ;;"的解决方案。在测试结束时。但是,结果不是我所期望的,所以我最终将其更改为:

SELECT at_award_description.ad_id, 
               at_award_description.aw_id, 
               at_award_description.ad_group, 
               at_award_description.ad_detail, 
               at_award_description.ad_start_date, 
               at_award_description.ad_archived_date,
    MAX(at_cub_award_date.cad_task_completion_date)
FROM at_award_description
LEFT JOIN at_cub_award_date ON ((at_award_description.ad_id = at_cub_award_date.ad_id)
    AND (at_cub_award_date.ca_id = 37))
WHERE at_award_description.aw_id = 5
GROUP BY at_award_description.ad_group
ORDER BY at_award_description.ad_order, at_award_description.ad_group

0 个答案:

没有答案