【发布时间】:2014-04-28 07:27:56
【问题描述】:
这是我的查询
SELECT CONCAT(`SM_Title`,' ',`SM_Full_Name`) AS NAME,
`RG_Date`,
`RG_Reg_No`,
`RG_Stu_ID`,
`SM_Tell_Mobile`,
`SM_Tel_Residance`,
`RG_Reg_Type`,
Default_Batch,
`RG_Status`,
`RG_Final_Fee`,
`RG_Total_Paid`,
(`RG_Final_Fee`-`RG_Total_Paid`) AS TOTALDUE,
SUM(`SI_Ins_Amount` - `SI_Paid_Amount`) AS AS_AT_APRIAL_END
INNER JOIN
(SELECT `SI_Ins_Amount`,
`SI_Reg_No`
FROM
`student_installments`
GROUP BY MONTHNAME(`SI_Due_Date`)) Z ON
Z.`SI_Reg_No` = `registrations`.`RG_Reg_No`
FROM `registrations`
LEFT JOIN `student_master` ON `student_master`.`SM_ID` = `registrations`.`RG_Stu_ID`
LEFT JOIN `student_installments` ON `student_installments`.`SI_Reg_No` = `registrations`.`RG_Reg_No`
WHERE (`RG_Reg_Type` LIKE '%HND%' OR `RG_Reg_Type` LIKE '%LMU%' )
AND `SI_Due_Date` <= '2014-04-30' GROUP BY `SI_Reg_No`
它在附近给了我一个错误
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 'Z LIMIT 0, 25' at line 1
【问题讨论】:
-
我猜
FROM registrations应该在选择列表之后首先加入。 -
除了 INNER JOIN 出现在 FROM 之前的问题之外,您的子查询似乎很可疑。您正在获取 SI_Reg_No 和 SI_Ins_Amount 但按 SI_Due_Date 分组。我假设 SI_Reg_No 指的是特定的学生,但是随着 group by 它将为所有学生分组(带回来的 SI_Reg_No 是未定义的)。此外,当您不返回此字段(而是返回 RG_Reg_No)时,您的主查询按 SI_Reg_No 分组,并且在 GROUP BY 上,您没有定义要使用的 SI_Reg_No 表值(即 z.SI_Reg_No 或 student_installments。 SI_Reg_No)。