Query between two tables is returning NULL value

Asked

Viewed 15 times

0

i am trying to make a query that will join some tables in my database using a Join. The tables are as follows::

Table 1 - RESULTADOS_SIGLAS

+--------------+-------------+-------+
| ID_RESULTADO |  RESULTADO  | SIGLA |
+--------------+-------------+-------+
|            1 | RESULTADO 1 | RES1  |
|            2 | RESULTADO 2 | RES2  |
|            3 | RESULTADO 3 | RES3  |
+--------------+-------------+-------+

Table 2 - FINAL RESULTS

+--------------+----------+----------+--------------+
| ID_RES_FINAL | ID_TURMA | ID_ALUNO | ID_RESULTADO |
+--------------+----------+----------+--------------+
|            1 |        1 |      123 |            2 |
|            2 |        2 |      321 |            3 |
|            3 |        3 |      444 |            2 |
+--------------+----------+----------+--------------+

What I need is to find the number of students who have a certain result and the information of that result (the result itself and its acronym), ex: Find all students in class 3 who have the result 1.

So, in my head, I should do the consultation as follows:

SELECT COUNT(RESULTADOS_FINAIS.ID_ALUNO) AS QUANTIDADE, RESULTADOS_SIGLAS.RESULTADO, RESULTADOS_SIGLAS.SIGLA
FROM RESULTADOS_SIGLAS 
INNER JOIN RESULTADOS_FINAIS on RESULTADOS_SIGLAS.ID_RESULTADO = RESULTADOS_FINAIS.ID_RESULTADO
WHERE RESULTADOS_FINAIS.ID_TURMA = 3
AND RESULTADOS_FINAIS.ID_RESULTADO = 1

What I wanted him to return to me in this hypothetical situation was:

+------------+-------------+-------+
| QUANTIDADE |  RESULTADO  | SIGLA |
+------------+-------------+-------+
|          0 | RESULTADO 1 | RES1  |
+------------+-------------+-------+

But he ended up returning me the following:

+------------+-----------+-------+
| QUANTIDADE | RESULTADO | SIGLA |
+------------+-----------+-------+
|          0 | NULL      | NULL  |
+------------+-----------+-------+

Could someone help me with this consultation? Thank you very much!

  • There is no row in your table that meets the condition: RESULTADOS_FINAIS.ID_TURMA = 3 AND RESULTADOS_FINAIS.ID_RESULTADO = 1 then the result is null (and only came line because of COUNT, otherwise neither return anything). The suggestion in these cases is to make the two queries work without JOIN, then, confirming that everything is right, join in a SELECT only.

No answers

Browser other questions tagged

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