我下面的脚本在内部查询上引发错误。我使用Sequel Pro,仅收到此错误:


  您的SQL语法有误;查看与您的MySQL服务器版本相对应的手册,以在'SELECT Savings附近使用正确的语法
  FROM OfferSuggestionHeader osh
  左加入项目p
  在osh.OfferI'在第2行


因此,我不确定实际问题是什么。我尝试在内部查询中进行显式内部联接,以防p别名未得到正确的引用/解释或类似的操作,但这没有积极作用。

SELECT SUM(osh.Savings) as YTD,
   SUM (SELECT Savings
FROM OfferSuggestionHeader osh
LEFT JOIN Projects p
ON osh.OfferID = p.offer_id
WHERE p.uid = 1 AND p.current_status < 3)
   AS "Open Savings"
FROM OfferSuggestionHeader osh
LEFT JOIN Projects p
    ON p.offer_id = osh.OfferID
WHERE p.uid = '1'


任何帮助表示赞赏。最好,

最佳答案

我认为您可以将查询编写得更简单:

SELECT SUM(osh.Savings) as YTD,
       SUM(case when p.current_status < 3 then Savings end) AS "Open Savings"
FROM OfferSuggestionHeader osh LEFT JOIN
     Projects p
     ON p.offer_id = osh.OfferID
WHERE p.uid = '1'


也就是说,您可以用条件求和完全替换子查询。

10-08 15:12