在PHP MySQL Left Join中使用COUNT两次

时间:2014-06-09 14:16:42

标签: php mysql sql

我在此查询中成功运行了以下COUNT个功能。我想在另一个名为HTG_ScheduledActual的表(格式相同)上发出完全相同的COUNT / Left Join功能

这有效:

$query_WADAHTG_TechProps =  "SELECT
        HTG_TechProps.EmpNumber, HTG_TechProps.EmpFirstName, HTG_TechProps.EmpLastName,  HTG_TechProps.Veh_Number,
        COUNT(HTG_ScheduleRequest.ID) AS current_job FROM HTG_TechProps
 LEFT JOIN HTG_ScheduleRequest ON HTG_TechProps.EmpNumber = HTG_ScheduleRequest.SSR 
    AND (HTG_ScheduleRequest.ScheduleDateOriginal = CURDATE() 
    OR HTG_ScheduleRequest.ScheduleDateCurrent = CURDATE() 
    OR HTG_ScheduleRequest.ScheduleDateExact = CURDATE() ) GROUP BY HTG_TechProps.EmpNumber 
    ORDER BY HTG_TechProps.EmpNumber ASC 
    ";

我尝试使用AND插入第二个COUNT,它会中断查询。我感觉不好,我不能在同一个查询中发出两个COUNT?如何克服这个问题是最好的方法?

$query_WADAHTG_TechProps =  "SELECT
        HTG_TechProps.EmpNumber, HTG_TechProps.EmpFirstName, HTG_TechProps.EmpLastName,  HTG_TechProps.Veh_Number,
        COUNT(HTG_ScheduleRequest.ID) AS current_job FROM HTG_TechProps
 LEFT JOIN HTG_ScheduleRequest ON HTG_TechProps.EmpNumber = HTG_ScheduleRequest.SSR 
    AND (HTG_ScheduleRequest.ScheduleDateOriginal = CURDATE() 
    OR HTG_ScheduleRequest.ScheduleDateCurrent = CURDATE() 
    OR HTG_ScheduleRequest.ScheduleDateExact = CURDATE() ) GROUP BY HTG_TechProps.EmpNumber
        AND COUNT(HTG_ScheduleActual.ID) AS actual_job FROM HTG_TechProps
 LEFT JOIN HTG_ScheduleActual ON HTG_TechProps.EmpNumber = HTG_ScheduleActual.SSR 
    AND (HTG_ScheduleActual.ScheduleDateOriginal = CURDATE() 
    OR HTG_ScheduleActual.ScheduleDateCurrent = CURDATE() 
    OR HTG_ScheduleActual.ScheduleDateExact = CURDATE() ) GROUP BY HTG_TechProps.EmpNumber  
    ORDER BY HTG_TechProps.EmpNumber ASC 
    ";

0 个答案:

没有答案