I have these four tables related to each other (ignore the relationships that go upwards).
I will use a query so that you can understand the problem, which is complex:
SELECT u.nombre, COUNT(fr.id_tipologia), t.nombre
FROM tipologias AS t
INNER JOIN fr
ON t.id = fr.id_tipologia
INNER JOIN encargos AS e
ON fr.id_encargo = e.id
INNER JOIN usuarios AS u
ON e.cod_usuario = u.cod_usuario
GROUP BY u.nombre, fr.id_tipologia
Reading the query would be something like: Tell me how many types and with what name each user is assigned.
This is the following:
For now all correct, the data in essence are what I need, the issue is that these data are to display them in a graph, so I need the null of relationships for data ordering issues when interpreting them in the graphic.
Specifically, the "question" I want to ask the database would be: What typologies are not assigned to each user? That is, the answer to that query would be something like this:
+------------------+------------------------+------------------+
| nombre | COUNT(fr.id_tipologia) | nombre |
+------------------+------------------------+------------------+
| Desarrollo | 29 | Plaza de parking |
+------------------+------------------------+------------------+
| Desarrollo | 22 | Vivienda |
+------------------+------------------------+------------------+
| Desarrollo | 4 | Oficina |
+------------------+------------------------+------------------+
| Desarrollo | 4 | Local |
+------------------+------------------------+------------------+
| Nombre de Testeo | 0 | Plaza de parking |
+------------------+------------------------+------------------+
| Nombre de Testeo | 1 | Vivienda |
+------------------+------------------------+------------------+
| Nombre de Testeo | 0 | Oficina |
+------------------+------------------------+------------------+
| Nombre de Testeo | 0 | Local |
+------------------+------------------------+------------------+
I have not managed to get it out with the left and the right join, and the outer joins can not be used in MySQL I understand.
I hope you can help me, thank you very much!
I also add the results with LEFT JOIN:
SELECT u.nombre, COUNT(fr.id_tipologia), t.nombre
FROM tipologias AS t
LEFT JOIN fr
ON t.id = fr.id_tipologia
LEFT JOIN encargos AS e
ON fr.id_encargo = e.id
LEFT JOIN usuarios AS u
ON e.cod_usuario = u.cod_usuario
GROUP BY u.nombre, fr.id_tipologia