I want to show in a table how many people belong to a department and their department head, if there are no people, it will show 0, and if there is no department head assigned, any value ("there is not", 0, I do not care)
In this selection I take the number of people associated with the departments, putting 0 if there is none:
SELECT d.Nombre_Depart as Departamento, Count(u.nombre) as total
from departamentos d left join usuarios u on d.Nombre_Depart=u.Departamento
GROUP BY d.Nombre_Depart order by total
In this select the heads of the department.
Select departamento,Nombre,Apellidos
from usuarios,departamentos,union_pers_rols
where usuarios.Departamento=departamentos.Nombre_Depart
and union_pers_rols.Id_usuario=usuarios.Correo
and usuarios.Departamento=departamentos.Nombre_Depart
and union_pers_rols.Id_rol='Jefe Departamento'
The problem is that I do not know how to combine the 2 Selects.
In the Users Table I have the fields: Name, Mail, Department
In the Department Table I have the fields: Nombre_Depart
Users throw the fk to Department
In the Role table I have the fields: type_rol.
In the table Union_pers_rol the fields: user_ID, Id_rol
Union_pers_rol throws the fk of user_ID to users.mail and from Id_rol to the table rol.type_rol