2017-06-14 167 views
0

我有這個MySQL查詢。它產生一個錯誤,Unknown column 'lp.amount' in 'field list'從多個表中選擇半連接

SELECT l.*, SUM(lp.amount) AS landPayMonth, p.pName, u.uName, SUM(ci.amount) AS totAmnt, t.unitId FROM (SELECT distinct landlord_payment.id FROM landlord_payment) lp, (SELECT distinct cashIn.id FROM cashIn) ci, landlords l, properties p, units u, tenants t WHERE ci.tenantId=t.id AND l.id=lp.landlordId AND t.unitId=u.id AND u.propertyId=p.id AND p.landlordId=l.id AND STR_TO_DATE(ci.registeredTime, "%Y-%m")=STR_TO_DATE(CURDATE(), "%Y-%m") 
GROUP BY l.id; 

請幫我。

+0

我不知道你到底想要做什麼。把一些樣本輸入和預期輸出。 –

回答

1
SELECT 
    l.*,SUM(lp.amount) AS landPayMonth,p.pName,u.uName,SUM(ci.amount) AS totAmnt, 
    t.unitId 
FROM 
    (SELECT DISTINCT landlord_payment.id,landlord_payment.amount,landlord_payment.landlordId FROM landlord_payment) lp, 
    (SELECT DISTINCT cashIn.id,cashIn.tenantId,cashIn.registeredTime,cashIn.amount FROM cashIn) ci,landlords l,properties p,units u,tenants t 
WHERE 
    ci.tenantId = t.id 
     AND l.id = lp.landlordId 
     AND t.unitId = u.id 
     AND u.propertyId = p.id 
     AND p.landlordId = l.id 
     AND STR_TO_DATE(ci.registeredTime, '%Y-%m') = STR_TO_DATE(CURDATE(), '%Y-%m') 
GROUP BY l.id; 

您可以嘗試上面的代碼。

希望這會幫助你。

+0

'ci.amount'會拋出錯誤。 –

+0

lp.landlordId引發錯誤。 – Mwotie

+0

嘗試更新一個..! 它解決了您的問題嗎? –