Help with Subquery in SQL?

Asked

Viewed 241 times

0

I have in my system a table called Agendas, this table is linked with other tables Class and School through the columns turmas_id and escola_id. What happens is that the column turmas_id is not a related Foreign key even getting the code from the Class table it can also receive 0, as I created 0 as a representation for sending message in the system to all the Classes of the referring school.

What happens is that now I need to display the data both for existing Class and for this that receives 0 referencing all Class School. If I use JOIN to join the tables only valid codes will be returned and the code 0 will be ignored because it does not exist in the table Class.

I thought about using Subquery to do this, but I haven’t been able to do it yet. What would be the best solution to make a SELECT returning the existing classes in the table Class for example, class 15 (third year) together with the Class that receives 0 symbolizing all ?

I am trying so with JOIN, but in this case the values in 0 are not returned.

$agendamentos = $this->Agenda->query("SELECT * FROM responsavel_alunos RespAlunos "
                    . "INNER JOIN pessoas Responsavel ON (Responsavel.id = RespAlunos.pessoas_id) "
                    . "INNER JOIN pessoas Aluno ON (Aluno.id = RespAlunos.pessoas_id1) "
                    . "INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id) "
                    . "INNER JOIN turmas Turma ON (Turma.id = Matricula.turmas_id) "
                    . "INNER JOIN escolas Escola ON (Escola.id = Matricula.escolas_id) "
                    . "INNER JOIN agendas Agenda ON (Agenda.turmas_id = Turma.id) "
                    . "WHERE Responsavel.id = ? ORDER BY Agenda.created DESC "
                    , array($id)); //id do responsavel

EDIT

$agendamentos = $this->Agenda->query("SELECT * FROM responsavel_alunos RespAlunos "
                    . "INNER JOIN pessoas Responsavel ON (Responsavel.id = RespAlunos.pessoas_id) "
                    . "INNER JOIN pessoas Aluno ON (Aluno.id = RespAlunos.pessoas_id1) "
                    . "INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id) "
                    . "LEFT JOIN turmas Turma ON (Turma.id = Matricula.turmas_id OR Turma.id = 0) "
                    . "INNER JOIN escolas Escola ON (Escola.id = Matricula.escolas_id) "
                    . "LEFT JOIN agendas Agenda ON (Agenda.turmas_id = Turma.id) "
                    . "WHERE Responsavel.id = ? ORDER BY Agenda.created DESC "
                    , array($id)); //id do responsavel

Image of the model

inserir a descrição da imagem aqui

  • If you can send the image of your modeling would help to map the tables and better understand your database to identify if there is no wrong relation that you are doing in your query.

  • 1

    @Adrianodeabreuwanderley posted the image of the model !

  • @Fernandopaiva, if it worked, mark the answer as accepted, so you can help other users.

3 answers

3

Do as Daniel Saraiva directed, using the left join . Check your query as it should look. I tested it here and it worked.

SELECT * FROM responsavel_alunos RespAlunos
    INNER JOIN pessoas Responsavel ON (Responsavel.id = RespAlunos.pessoas_id)
    INNER JOIN pessoas Aluno ON (Aluno.id = RespAlunos.pessoas_id1)
    INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id)
    LEFT JOIN turmas Turma ON (Turma.id = Matricula.turmas_id)
    INNER JOIN escolas Escola ON (Escola.id = Matricula.escolas_id)
    LEFT JOIN agendas Agenda ON (Agenda.turmas_id = Turma.id)
WHERE Responsavel.id = ?
ORDER BY Agenda.created DESC
  • Even using LEFT JOIN Classes in Schedules that are set to 0 do not return. I need you to return the Schedules with a valid Class(with id) and those with Class with id 0 in Agenda. Understand what I need ?

  • substitute inner join for left join in agendas. and try again.

1

use the left join that will work example

select USUARIO.USU_CODIGO, USU_NOME, GRU_CODIGO from USUARIO left join USUGRUPO ON USUARIO.USU_CODIGO = USUGRUPO.USU_CODIGO
  • I edited my question with the SELECT example I’m using, I tried using LEFT but I still can’t return the values that contain Class = 0

0

Test this sql using class or.id = 0;

    SELECT * FROM responsavel_alunos RespAlunos
    INNER JOIN pessoas Responsavel ON (Responsavel.id = RespAlunos.pessoas_id)
    INNER JOIN pessoas Aluno ON (Aluno.id = RespAlunos.pessoas_id1) 
    INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id) 
    INNER JOIN turmas Turma ON (Turma.id = Matricula.turmas_id or turma.id = 0) 
    INNER JOIN escolas Escola ON (Escola.id = Matricula.escolas_id) 
    INNER JOIN agendas Agenda ON (Agenda.turmas_id = Turma.id) 
     WHERE Responsavel.id = ? ORDER BY Agenda.created DESC;
  • in case I would have to use in Agenda JOIN the 0 is only in this table. I tried to use Agenda.turmas_id = Turma.id OR Agenda.turmas_id = 0 but messes up all the results. In case picks up student enrolled in another Class.

  • Try to put LEFT JOIN in the class table and agenda table, and leave only the classes.id = 0 in the class LEFT.

  • I edited the Post with the information I think you suggested. If this is how you suggested the column turmas_id that has 0 returns null without Schedule Description.

Browser other questions tagged

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