2014-04-28 87 views
-1

這是我的查詢使用子查詢加入MySQL的

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

+2

我猜'從註冊'應該高於選擇列表後第一次加入。 –

+0

除了FROM之前INNER JOIN的問題之外,您的子查詢似乎很可疑。您正在獲取SI_Reg_No和SI_Ins_Amount,但是按SI_Due_Date分組。我假設SI_Reg_No指的是特定的學生,但是由它組成的小組將爲所有學生組成(其中SI_Reg_No帶回的是未定義的)。此外,當您不返回此字段(而您返回RG_Reg_No)時,您的主要查詢有一個由SI_Reg_No組成的組,而在GROUP BY上,您不定義要使用的SI_Reg_No的哪個表值(即,z.SI_Reg_No或student_installments。 SI_Reg_No)。 – Kickstart

回答

0
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 
FROM registrations 
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 
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 
0

我注意到你已經fogotten左表或子查詢,你想要加入到(SELECT SI_INs .....)並且之前我可以看到在加入之前沒有from子句。

我希望這可以幫助你 問候

0

您在錯誤的位置子句中使用它應該只是你列的選擇後,您可以使用下面的查詢:

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 
FROM registrations AS reg 
JOIN 
(SELECT 
SI_Ins_Amount,SI_Reg_No 
FROM student_installments 
GROUP BY MONTHNAME(SI_Due_Date)) AS Z 
ON Z.SI_Reg_No = reg.RG_Reg_No 
LEFT JOIN student_master AS sm 
ON sm.SM_ID = reg.RG_Stu_ID 
LEFT JOIN student_installments AS si 
ON si.SI_Reg_No = reg.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; 
0

在下面的部分,從關鍵字應該去之前的內部加入:

FROM registrations 
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