mysql select 中的子查询加入

mysql subquery in select join

我对以下语句有疑问:

SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId 
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
FROM foreseen_charges GROUP BY foreseen_charges.flatsId

我总是收到这个错误:

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 'LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps ON f' at line 2

谁能帮帮我?

此致, CS

FROM 放在 LEFT JOIN

之前
SELECT SUM(foreseen_charges.commonCharge) as required, 
foreseen_charges.flatsId 
FROM foreseen_charges
LEFT JOIN (
    SELECT deposits.flatsId 
    FROM deposits 
    GROUP BY flatsId
) deps ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId

你做事乱七八糟。大体结构为:

SELECT (something) FROM table JOIN other-table ON table.var1=other-table.var2 WHERE situation

您正在这样做:

SELECT (something) LEFT JOIN table ON table.var1=other-table.var2 FROM other-table 

您必须声明两个 table 才能加入,并且您不能关联其他 table 因为您还没有声明它。

不确定查询的目的是什么,但这应该可以解决您的语法错误:

SELECT SUM(foreseen_charges.commonCharge) as required, foreseen_charges.flatsId 
FROM foreseen_charges 
LEFT JOIN (SELECT deposits.flatsId FROM deposits GROUP BY flatsId) deps
ON foreseen_charges.flatsId = deps.flatsId
GROUP BY foreseen_charges.flatsId