2012-12-18 72 views
0

我有兩個MySQL表,合併兩個MySQL表並得到結果放入數據網格在VB.NET

tblloanRegistry

LoanID  EMPNumber  Date  Amount  Status 
1   1111   2012-10-01 50000  0 
2   2222   2012-10-10 10000  1 

tblLoanAccount

ID  LoanID  Date  Payment  Interest  Total  Auto  Installment 
1   1  2012-10-25 5000   0   5000  0   1 
2   1  2012-11-01  0  100   100  1   0 
3   1  2012-11-25 5000  100  5100  0   2 
4   2  2012-11-25 1500   0   1500  0   1 

輸出爲成員1111:

Date   Description  Principle Interest  Balance 
2012-10-25 Installment: 1  5000   0   45000 
2012-11-01 Interest    0   100   45100 
2012-11-25 Installment: 2  5000   100   40000 

I嘗試以下,但它顯示一個錯誤。

SELECT tblLoanAccount.Date, tblLoanAccount.Payment, tblLoanAccount.Interest, 
tblLoanAccount.Total, tblLoanAccount.Auto, tblLoanAccount.Installment FROM " & 
"tblLoanAccount WHERE tblLoanAccount.EMPNumber=" & cmbEMPNumber.Text & " AND 
tblLoanAccount.LoanID = '1' AND tblLoanAccount.Total <> 0 ORDER BY tblLoanAccount.ID 

錯誤:

enter image description here

+0

從您的描述中,它聽起來像是一個VB.NET語法錯誤。嘗試將整個字符串放在一行上。無論如何,請提供有關此錯誤的更多詳細信息:它是語法錯誤還是運行時錯誤? –

+0

此外,請注意最佳做法是使用[參數](http://msdn.microsoft.com/en-us/library/B623F810-D871-49A5-B0F5-078CC3C34DB6(v = vs.100,d = lightweight ).aspx)而不是將字符串連接到SQL語句中。 –

+0

我已經添加了上面的錯誤。我對VB.NET很陌生。如果你能指導我參加任何教程,這將是很大的幫助。 –

回答

1

此錯誤是因爲你還沒有加入的表。

使用此查詢爲您的期望答案。你應該加入這兩個表,然後只有你可以得到輸出。

SELECT tblLoanAccount.Date, tblLoanAccount.Payment, tblLoanAccount.Interest, 
    tblLoanAccount.Total, tblLoanAccount.Auto, tblLoanAccount.Installment,  
    if(Installment = 0, 'Interest', concat('Installment : ', Installment)) as Description 
FROM tblLoanAccount 
JOIN tblloanRegistry ON tblloanRegistry.LoanID = tblLoanAccount.LoanID 
WHERE tblloanRegistry.EMPNumber= 1111 
    AND tblLoanAccount.LoanID = 1 
    AND tblLoanAccount.Total <> 0 
ORDER BY tblLoanAccount.ID 

你的表模式在this link創建。請通過它。