Return data difference from two sql tables

Asked

Viewed 53 times

2

I have a table ESCOLA with ID_ESCOLA and NOME_ESCOLA and another table PROFESSORES containing a FK_ID_ESCOLA and NOME_PROFESSOR. I wanted a table that would return me the names of all schools but schools that had no teachers would return with another color. I know how to handle this data, I just don’t know how to assemble the .

Thank you all.

2 answers

2

Make a COUNT teachers to know when there is and when there is not, example:

SELECT e.nome_escola, COUNT(p.nome_professor) qtdeProfessores
FROM ESCOLA e
LEFT JOIN PROFESSORES p ON p.ID_ESCOLA = e.ID_ESCOLA
GROUP BY e.nome_escola

That way when you come qtdeProfessores = 0, then you display one color, when it comes <> 0 shows another.

1


Try the following:

SELECT Escola.nome_escola, count(Professores.ID)
FROM Escolas
LEFT JOIN Professores
ON Escolas.ID_ESCOLA=Professores.FK_ID_ESCOLA
GROUP BY Escolas.ID_ESCOLA;

Browser other questions tagged

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