SELECT a.*, b.Amount,c.* FROM
(
SELECT ProjectCode, TypeCode,TransType, Sum(AmountCash) as SumAmount
FROM cashflow GROUP BY ProjectCode, TypeCode, TransType
) as a
inner join table2 as b on a.ProjectCode = b.ProjectCode
inner join Department as c on b.DepartmentCode = c.DepartmentCode
$sql="SELECT o.DeptCode,count(o.ProjectCode),sum(o.Amount),sum(cf.AmountCash),d.DeptName
FROM
(SELECT cf.ProjectCode, cf.TypeCode,cf.TransType, sum(cf.AmountCash)
FROM cashflow GROUP BY cf.ProjectCode,cf.TypeCode, cf.TransType
WHERE (cf.TypeCode = 'A') AND (cf.TransType ='I')) as cf
INNER JOIN openproject AS o ON cf.ProjectCode = o.ProjectCode
INNER JOIN department AS d ON o.DeptCode = d.DeptCode
GROUP BY d.DeptName,o.DeptCode
ORDER by o.DeptCode ASC";
แล้วฟ้อง error
Error : SELECT o.DeptCode,count(o.ProjectCode),sum(o.Amount),sum(cf.AmountCash),d.DeptName FROM (SELECT cf.ProjectCode, cf.TypeCode,cf.TransType, sum(cf.AmountCash) FROM cashflow GROUP BY cf.ProjectCode,cf.TypeCode, cf.TransType WHERE (cf.TypeCode = 'A') AND (cf.TransType ='I')) as cf INNER JOIN openproject AS o ON cf.ProjectCode = o.ProjectCode INNER JOIN department AS d ON o.DeptCode = d.DeptCode GROUP BY d.DeptName,o.DeptCode ORDER by o.DeptCode ASCYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (cf.TypeCode = 'A') AND (cf.TransType ='I')) as cf INNER JOIN o' at line 5
ต้องแก้ยังไงค่ะ ไม่ได้จริง ๆค่ะ
$sql="SELECT o.DeptCode,count(o.ProjectCode),sum(o.Amount),sum(cf.AmountCash),d.DeptName
FROM
(SELECT cf.ProjectCode, cf.TypeCode,cf.TransType, sum(cf.AmountCash) FROM cashflow GROUP BY cf.ProjectCode,cf.TypeCode, cf.TransType WHERE (cf.TypeCode = 'A') AND (cf.TransType ='I')) as cf
INNER JOIN openproject AS o ON cf.ProjectCode = o.ProjectCode
INNER JOIN department AS d ON o.DeptCode = d.DeptCode
GROUP BY d.DeptName,o.DeptCode
ORDER by o.DeptCode ASC";