我有两张桌子:
GererLocationAppart(IdAppartL, MatriculeEmploye)
Employe(MatriculeEmploye, PrenomEmploye, NomEmploye, NumeroTelephoneEmploye)
我想得到一张包含“PrenomEmploye”,“NomEmploye”和“Id AppartL”栏目的表格。最后一列应该具有每个“MatriculeEmploye”所指的行数。 我知道我应该在
之间做一个混合Select Distinct PrenomEmploye, NomEmploye, IdAppartL
From Employe E1, GererLocationAppart G1
Where E1.MatriculeEmploye=G1.MatriculeEmploye;
并且
SELECT MatriculeEmploye, COUNT(IdAppartL)
FROM GererLocationAppart
GROUP BY MatriculeEmploye;
但我不知道怎么做.. 我试试:
Select Distinct PrenomEmploye, NomEmploye
From Employe
Where MatriculeEmploye in
(SELECT MatriculeEmploye, COUNT(IdAppartL)
FROM GererLocationAppart
GROUP BY MatriculeEmploye);
但我明白了:
ERROR 1241 (21000): Operand should contain 1 column(s)
答案 0 :(得分: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;
答案 1 :(得分: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 e.MatriculeEmploye, e.Prenom, ...
)。