2012-12-18 22 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创建。请通过它。