0

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)
4

2 回答 2

1
SELECT
 E1.PrenomEmploye
,E1.NomEmploye
,E1.IdAppartL
,COUNT(G1.IdAppartL) as CountMatriculeEmploye
From Employe E1
JOIN GererLocationAppart G1 ON E1.MatriculeEmploye=G1.MatriculeEmploye
GROUP BY E1.MatriculeEmploye;
于 2013-04-17T12:59:54.397 回答
1

之后的子查询IN必须只返回 1 列。您要返回MatriculeEmploye, COUNT(IdAppartL)2 列。您的查询应该是这样的:

Select e.PrenomEmploye, e.NomEmploye, COUNT(g.IdAppartL)
From Employe e 
  INNER JOIN GererLocationAppart g ON g.MatriculeEmploye=e.MatriculeEmploye
GROUP BY 1,2;

请注意,此查询将合并具有相同 (Prenom, Nom) 对的员工,因此您可能还希望将 e.MatriculeEmploye 添加到 SELECT 和 GROUP BY 子句(GROUP BY 1,2,3如果您将 select 更改为SELECT e.MatriculeEmploye, e.Prenom, ...)。

于 2013-04-17T13:01:23.273 回答