[英]Inner Join in VB.Net
#Region "FillListView"
Sub FillListview()
LV.Items.Clear()
myqry = "SELECT AccResult.StudNo,Exercises.ID from AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.ID ORDER BY AccResult.FirstName,AccResult.YrandSec Asc;"
mycmd = New OleDbCommand(myqry, con)
con.Open()
mydr = mycmd.ExecuteReader
While mydr.Read
With LV
.Items.Add(mydr("StudNo"))
With .Items(.Items.Count - 1).SubItems
.Add(mydr("CNumber"))
.Add(mydr("FirstName"))
.Add(mydr("LastName"))
.Add(mydr("YrandSec"))
.Add(mydr("Exer1"))
.Add(mydr("Exer2"))
.Add(mydr("Exer3"))
.Add(mydr("Exer4"))
.Add(mydr("Exer5"))
End With
End With
End While
con.Close()
End Sub
#End Region
AccResult
is the name of my first table and Exercises
is the second. AccResult
是我的第一个表的名称,而Exercises
是第二个。 My PK for AccResult
is StudNo
and for Exercises
is ID
. 我的AccResult
PK是StudNo
,而Exercises
是ID
。 How can I join these two tables to display in ListView
? 如何将这两个表连接到ListView
?
AccResult
Table: AccResult
表:
StudNo (PK)
CNumber
FirstName
LastName
YrandSec
Exercises
Table: Exercises
表:
ID (PK)
StudNo
Exer1
Exer2
Exer3
Exer4
Exer5
Your JOIN condition is incorrect. 您的JOIN条件不正确。
AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.ID
is joining the primary key of the parent table (AccResult.StudNo) with the primary key of the child table (Exercises.ID). 正在使用子表的主键(Exercises.ID)加入父表(AccResult.StudNo)的主键。 You need to join the primary key of the parent table (AccResult.StudNo) with the foreign key of the child table (Exercises.StudNo), ie, 您需要将父表(AccResult.StudNo)的主键与子表的外键 (Exercises.StudNo)连接,即,
AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.StudNo
Not sure if I get the question correctly, but it looks like the query only select to columns : 不确定我是否正确地得到了问题,但看起来查询只选择列:
myqry = "SELECT AccResult.StudNo,Exercises.ID from AccResult..."
then the code try to display all columns. 然后代码尝试显示所有列。 Try to select all columns instead : 尝试选择所有列:
myqry = "SELECT * from AccResult..."
尝试这个:
myqry = "SELECT AccResult.StudNo,Exercises.ID, Exer1, Exer2, Exer3, Exer4, Exer5 from AccResult INNER JOIN Exercises ON AccResult.StudNo = Exercises.ID ORDER BY AccResult.FirstName,AccResult.YrandSec Asc;"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.