WHERE子句中的MySQL多个表

时间:2014-08-05 21:31:44

标签: mysql sql join

我获得了一个大学校友的MySQL数据库,记录了学生毕业后的职业。有三个表关注我(我已经简化了它们):

  1. 学生(StudentID,GraduationYear)
  2. StudentOccupations(StudentID,OccupationName,OccupationChange1,OccupationChange2)
  3. OccupationsClassified(OccupationName,Sector,Status)
  4. 我正在制作一个网络表单,以便学院内的人可以查询这些表格。特别是他们想要选择毕业年份范围和职业名称,以便提出类似的问题。" 1970年至1980年期间毕业的人有多少人担任过学校教师?"

    我的SQL就是:

    SELECT COUNT(DISTINCT `Students`.`StudentID`) AS `Students`
    FROM `Students`
    INNER JOIN `StudentOccupations` ON `Students`.`StudentID`=`StudentOccupations`.`StudentID`
    INNER JOIN `OccupationsClassified` ON  `StudentOccupations`.`OccupationName`=`OccupationsClassified`.`OccupationName` 
    WHERE `Students`.`GraduationYear` BETWEEN 1970 AND 1980
    AND 'School teacher' 
    IN (`StudentOccupations`.`OccupationName`,`StudentOccupations`.OccupationChange1`, `StudentOccupations`.`OccupationChange2`);
    

    该查询似乎没有问题,但他们也希望按行业搜索,并询问" 1970年至1980年期间毕业的人有多少曾在教育部门工作过?"我希望用上面查询的前几行开始这个查询,因为我可以用PHP条件构造查询(如果在表单中选择了这个元素,那么添加这个MySQL子句等)。但是,我无法解决这个问题。

    这是我最近的努力(它还没有计算记录,并且它已经偏离了上述查询):

    SELECT  `StudentOccupations`.`OccupationName`,`StudentOccupations`.`OccupationChange1`, `StudentOccupations`.`OccupationChange2`
    FROM `StudentOccupations`
    INNER JOIN `Students` ON `StudentOccupations`.`WebID` = `Students`.`WebID`
    INNER JOIN `OccupationsClassified` ON `StudentOccupations`.`OccupationName`=`OccupationsClassified`.`OccupationName`
    WHERE `Students`.`GraduationYear` BETWEEN 1970 AND 1980
    AND EXISTS (SELECT `OccupationsClassified`.`OccupationName` FROM `OccupationsClassified` WHERE `OccupationClassified`.`Sector` = 'Education')
    

    正如你所看到的,我有点挣扎!有没有人有任何想法我如何按职业部门过滤结果?

1 个答案:

答案 0 :(得分:0)

您可以在所有职业上加入OccupationsClassified,如下所示:

SELECT
  COUNT(DISTINCT `Students`.`StudentID`) AS `Students`
FROM `Students`
  INNER JOIN `StudentOccupations` ON `Students`.`StudentID` = `StudentOccupations`.`StudentID`
  LEFT JOIN `OccupationsClassified` initialOc
    ON `StudentOccupations`.`OccupationName` = `OccupationsClassified`.`OccupationName`
  LEFT JOIN `OccupationsClassified` secondOc
    ON `StudentOccupations`.`OccupationChange1` = `OccupationsClassified`.`OccupationName`
  LEFT JOIN `OccupationsClassified` thirdOc
    ON `StudentOccupations`.`OccupationChange2` = `OccupationsClassified`.`OccupationName`
WHERE
  `Students`.`GraduationYear` BETWEEN 1970 AND 1980
  AND 'Education' IN (initialOc.Sector, secondOc.Sector, thirdOc.Sector);

或者您已DISTINCT中已有COUNT

SELECT
  COUNT(DISTINCT `Students`.`StudentID`) AS `Students`
FROM `Students`
  INNER JOIN `StudentOccupations` ON `Students`.`StudentID` = `StudentOccupations`.`StudentID`
  INNER JOIN `OccupationsClassified` ON `OccupationsClassified`.`OccupationName` IN
                                        (`StudentOccupations`.`OccupationName`, `StudentOccupations`.`OccupationChange1`, `StudentOccupations`.`OccupationChange2`)
WHERE
  `Students`.`GraduationYear` BETWEEN 1970 AND 1980
  AND `OccupationsClassified`.`Sector` = 'Education';