0
The problem is that this returns me 8 records while in the database there are only 2. That is to say 4 for each 1. I cannot understand why. (If you want to edit the title of the question)
I got this comic:
And I have this select:
select alunos.numero,faltas_licao.tipo
from turma,alunos,disciplinas,licao,faltas_licao
where faltas_licao.aluno_id = alunos.id_aluno
and licao.id_turma = turma.id_turma
and faltas_licao.id_disciplina = licao.id_disciplina
and licao.id_disciplina = disciplinas.id_disciplina
and faltas_licao.licao = '" & currentlicao & "'
and disciplinas.nome = '" & F_Topo.CB_Disciplina.SelectedItem.ToString & "'
and turma.nome = '" & F_Topo.CB_Turma.SelectedItem.ToString & "'
and alunos.Numero = '" & aluno & "'"
Here is the code "all":
Dim con3 As New SqlConnection(cs)
Dim currentlicao As Integer = get_licao()
'MsgBox("current: " & currentlicao)
Using con3
Dim aluno As Integer = linha.Index
Dim sql As String = "select alunos.numero,faltas_licao.tipo from turma,alunos,disciplinas,licao,faltas_licao where faltas_licao.aluno_id = alunos.id_aluno and licao.id_turma = turma.id_turma and faltas_licao.id_disciplina = licao.id_disciplina and licao.id_disciplina = disciplinas.id_disciplina and faltas_licao.licao = '" & currentlicao & "' and disciplinas.nome = '" & F_Topo.CB_Disciplina.SelectedItem.ToString & "' and turma.nome = '" & F_Topo.CB_Turma.SelectedItem.ToString & "' and alunos.Numero = '" & aluno & "'"
'MsgBox(sql)
Dim sqlCom As New SqlCommand(sql, con3)
con3.Open()
Dim dr3 As SqlDataReader = sqlCom.ExecuteReader()
Dim miau As Integer = 0
If dr3.HasRows Then
While dr3.Read()
miau = miau + 1
'F_Principal.DataGridView2.Rows.Add(F_Principal.DataGridView2.RowCount + 1, dr3.Item(0))
MsgBox(miau)
MsgBox("ID: " & dr3.Item(0) & "TIPO:" & dr3.Item(1))
End While
Else
' Aqui faça o que quiser caso não tenha linha '
End If
dr3.Close()
Data in the database:
I do not understand how he returns me four records(alias 8 records, 4 being repeated).
Someone with database experience can detect my error?
EDIT:
Error:
System.Data.Sqlclient.Sqlexception (0x80131904): The Objects "faltas_licao" and "faltas_licao" in the FROM clause have the same Exposed Names. Use correlation Names to distinguish them. in System.Data.Sqlclient.SqlConnection.Onerror(Sqlexception Exception, Boolean breakConnection, Action
1 wrapCloseInAction) em System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) em System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) em System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) in System.Data.Sqlclient.SqlDataReader.Tryconsumemetadata() in System.Data.Sqlclient.SqlDataReader.get_MetaData() em System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) em System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) in System.Data.Sqlclient.SqlCommand.Executereader(Commandbehavior behavior, String method) in System.Data.Sqlclient.SqlCommand.Executereader() F_container.get_faults() in C: Users Alunop Desktop Work module 16 Work mod 16 Work mod 16 Work mod 16 F_container.Vb:line 163 ClientConnectionId:03d47535-a3d9-4723-b704-2649901d693e Error Number:1013,State:1,Class:16
Your query is wrong. Try using some INNER JOIN.
– gmsantos
https://www.youtube.com/watch?v=468_48aSz68 - I will use this as a reference.
– lemario
And thank you :)...
– lemario
@gmsantos, replace a JOIN inplicito for an explicit will not correct the query let alone improve the performance, the only gain here would be readability.
– Tobias Mesquita
@lemario, could you say what the intention of the consultation is? would you like to take the total of students' absences? whether per student or class? in any case, I advise you not to assemble your query concatenating strings, parametrize the same.
– Tobias Mesquita
@Tobymosque What I intend to do is to certain Tuma, discipline, lyco and student receive the type of foul, to then put in a table the fouls for each student.
– lemario