1

我有一个人的名单,他们的职业如下:

Doctor Male
Teacher  Male
Principal Male
Nurse Female
Doctor Male

当我按专业对它们进行分组时,我希望 Access 为不在我拥有的子列表中的任何专业返回 0。这是我正在使用的代码:

SELECT Profession.Profession, Count(Profession.Profession) AS CountOfProfession
FROM People INNER JOIN Profession ON People.PeopleID = Profession.PeopleID
WHERE (((People.Sex)="Male"))
GROUP BY Profession.Profession;

我希望它为医生产生 2,因为有 2 名男性,但也为护士产生 0,因为没有男性护士。请让我知道我该怎么做,谢谢!

4

2 回答 2

0

你需要一个外部连接。我切换了表格的顺序,所以它是left join

SELECT Profession.Profession,
       sum(iif(people.sex = "Male", 1, 0)) AS CountOfProfession
FROM Profession LEFT JOIN
     ON People.PeopleID = Profession.PeopleID 
GROUP BY Profession.Profession;
于 2013-04-02T18:56:19.247 回答
0

这可以通过外部连接并将条件从子句移动WHEREON子句来完成:

SELECT Profession.Profession, 
       COUNT(People.PeopleID) AS CountOfProfession
FROM Profession 
  LEFT JOIN People 
    ON (  People.PeopleID = Profession.PeopleID
      AND People.Sex = 'Male' )
GROUP BY Profession.Profession ;
于 2013-04-02T19:15:00.690 回答