I have two tables :
GererLocationAppart(IdAppartL, MatriculeEmploye)
Employe(MatriculeEmploye, PrenomEmploye, NomEmploye, NumeroTelephoneEmploye)
I would like to get a table wich contains "PrenomEmploye", "NomEmploye" and a column "Id AppartL". This last column should have the number of lines that every "MatriculeEmploye" are referring to. I know that i should do a mixture between
Select Distinct PrenomEmploye, NomEmploye, IdAppartL
From Employe E1, GererLocationAppart G1
Where E1.MatriculeEmploye=G1.MatriculeEmploye;
And
SELECT MatriculeEmploye, COUNT(IdAppartL)
FROM GererLocationAppart
GROUP BY MatriculeEmploye;
But i don't know how.. I try :
Select Distinct PrenomEmploye, NomEmploye
From Employe
Where MatriculeEmploye in
(SELECT MatriculeEmploye, COUNT(IdAppartL)
FROM GererLocationAppart
GROUP BY MatriculeEmploye);
But I get :
ERROR 1241 (21000): Operand should contain 1 column(s)
The subquery after IN
must return only 1 column. You are returning MatriculeEmploye, COUNT(IdAppartL)
so 2 columns. Your query should be something like:
Select e.PrenomEmploye, e.NomEmploye, COUNT(g.IdAppartL)
From Employe e
INNER JOIN GererLocationAppart g ON g.MatriculeEmploye=e.MatriculeEmploye
GROUP BY 1,2;
Note that that this query will merge employees that have the same (Prenom, Nom) couple, so you may also want to add e.MatriculeEmploye to the SELECT and GROUP BY clause (GROUP BY 1,2,3
if you change your select to SELECT e.MatriculeEmploye, e.Prenom, ...
).