所以這裏是我的代碼如下。子查詢返回多於1行?
qry = "SELECT * FROM `tblsubjects` WHERE `GRADELEVEL` = " & lblgrade.Text & " AND `SUBJECTNAME` <> (SELECT `SUBJECTNAME` FROM `tblschedule` WHERE `SECTIONNAME` = '" & lblsect.Text & "')"
它來自may vb應用程序。我認爲錯誤來自查詢。我正在制定一個調度系統。在那個查詢中,我試圖加載尚未在該級別上安排的主題,並將該部分加載到組合框上。但是當我嘗試在本節中添加更多時間表時,出現此錯誤。
enter image description here 這是我對這個功能整個代碼:
Private Sub subjectload()
con = New MySqlConnection
con.ConnectionString = "server=localhost;user id=root;password='';database=kccbeslis"
If DataGridView.Rows.Count > 0 Then
Try
con.Open()
qry = "SELECT * FROM `tblsubjects` WHERE `GRADELEVEL` = " & lblgrade.Text & " AND `SUBJECTNAME` <> (SELECT `SUBJECTNAME` FROM `tblschedule` WHERE `SECTIONNAME` = '" & lblsect.Text & "')"
cmd = New MySqlCommand(qry, con)
rdr = cmd.ExecuteReader
While rdr.Read
Dim subname = rdr.GetString("SUBJECTNAME")
cbsubject.Items.Add(subname)
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ErrorToString)
End Try
Else
Try
con.Open()
qry = "SELECT * FROM `tblsubjects` WHERE `GRADELEVEL` = " & lblgrade.Text & ""
cmd = New MySqlCommand(qry, con)
rdr = cmd.ExecuteReader
While rdr.Read
Dim subname = rdr.GetString("SUBJECTNAME")
cbsubject.Items.Add(subname)
End While
con.Close()
Catch ex As Exception
MessageBox.Show(ErrorToString)
End Try
End If
End Sub
幫我請!
最有可能的是,您只需要用'NOT IN'替換'<>'。 – Uueerdo
嘗試在您的MS SQL子查詢中使用TOP 1,感謝很多先生,對於mysql –