Group in mysql together with Join

Asked

Viewed 144 times

0

I have a class table and a table of parents. A student may be associated with up to three parents.

SELECT 
      a.idAluno,
      a.matriculaAluno,
      a.nomeAluno,
      a.sexoAluno,
      a.dataNascAluno,
      b.id,
      b.matricula,
      b.cpf,
      b.nome 
    FROM 
      alunos a 
      LEFT JOIN matriculas b 
      ON a.matriculaAluno = b.matricula 
    WHERE 
      a.status = 1 AND b.status = 1 
     GROUP by 
        b.cpf

The return is : inserir a descrição da imagem aqui I would like the same records from the first table not to be repeated. And I want the matriculation table to come in the grouped survey.

That is, for each student record come grouped the three records of responsible, because each student can have 3 responsible at most.

2 answers

0

The problem of grouping by Idaluno is that the return will be only the first, ignoring the others responsible. The fact is that it is not a problem to repeat the first information of the table, and the others will come different, just ignore them in the treatment of information. Your select is already right.

0

Not to repeat the first column You need a group by de idAluno and take the group by from Cpf. Normally cpfs are unique and it doesn’t make much sense to group by in them but would lose the different cpfs when giving a group by in idSo what seems wrong to me is these values because equal ids for different students does not make sense. Finally to bring the registration just do in select a *.matricula

SELECT 
      b.*,
      a.idAluno,
      a.matriculaAluno,
      a.nomeAluno,
      a.sexoAluno,
      a.dataNascAluno,
      b.id,
      b.matricula,
      b.cpf,
      b.nome 
    FROM 
      alunos a 
      LEFT JOIN matriculas b 
      ON a.matriculaAluno = b.matricula 
    WHERE 
      a.status = 1 AND b.status = 1 
     GROUP by 
        a.idAluno

Browser other questions tagged

You are not signed in. Login or sign up in order to post.