如果sql用COUNT()计算了特定的人,我怎么知道这些选定的人使用他们的id?

时间:2014-05-19 03:25:12

标签: php sql arrays

如果sql使用COUNT()计算特定人员,我应该如何知道这些选定人员使用此查询使用他们的ID? 例如,如果它计算了5个nonhmsonboard,那么他们/他们的ID是什么?

                SELECT long_posname,            
                COUNT( 
                CASE WHEN employment_status LIKE  'ON BOARD' and trim(groupID)='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) AS  'hmsonboard',

                COUNT( 
                CASE WHEN employment_status LIKE  'VACATION' and trim(groupID)='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) AS  'hmsonvacation',

                COUNT( 
                CASE WHEN employment_status LIKE  'ON BOARD' and trim(groupID)='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) + COUNT( 
                CASE WHEN employment_status LIKE  'VACATION' and trim(groupID)='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) AS  'hmstotal',

                COUNT( 
                CASE WHEN employment_status LIKE  'ON BOARD' and trim(groupID)!='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) AS  'nonhmsonboard',

                COUNT( 
                CASE WHEN employment_status LIKE  'VACATION' and trim(groupID)!='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) AS  'nonhmsonvacation',

                COUNT( 
                CASE WHEN employment_status LIKE  'ON BOARD' and trim(groupID)!='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) + COUNT( 
                CASE WHEN employment_status LIKE  'VACATION' and trim(groupID)!='grp320952d8919d763a8'
                THEN 1 
                ELSE NULL 
                END ) AS  'nonhmstotal',

                COUNT( 
                CASE WHEN employment_status LIKE  'ON BOARD'
                THEN 1 
                ELSE NULL 
                END ) + COUNT( 
                CASE WHEN employment_status LIKE  'VACATION'
                THEN 1 
                ELSE NULL 
                END ) AS  'total', crewID

                FROM (

                SELECT tb_crew_master.groupID, tb_crew_master.crewID, MAX( date_arrive ) , tb_crew_master.empstatID, pl_employment_status.employment_status, long_posname, pos_arrangement, tb_crew_master.posID
                FROM tb_crew_master
                INNER JOIN tb_crew_seaservice ON tb_crew_master.crewID = tb_crew_seaservice.crewID
                INNER JOIN pl_position ON pl_position.posID = tb_crew_master.posID
                INNER JOIN pl_employment_status ON pl_employment_status.empstatID = tb_crew_master.empstatID
                INNER JOIN tb_vessel ON tb_vessel.vesID = tb_crew_seaservice.vesID 
                INNER JOIN pl_vessel_type ON pl_vessel_type.vestypeID = tb_vessel.vestypeID
                WHERE tb_crew_master.is_inactive = 'N' and pl_vessel_type.vestypeID='typ249225370585e575f8'
                GROUP BY tb_crew_seaservice.crewID 
                ) AS t
                GROUP BY posID
                ORDER BY pos_arrangement

1 个答案:

答案 0 :(得分:0)

不确定但使用crewID列(假设来自您发布的查询代码)或其他任何内容 相应表格中的ID列(来自pl_employment_status表的ID列),如

            SELECT crewID,
            long_posname,            
            COUNT( 
            CASE WHEN employment_status LIKE  'ON BOARD' 
            and trim(groupID)='grp320952d8919d763a8'
            THEN 1 
            ELSE NULL 
            END ) AS  'hmsonboard',

            .......

             <Rest of your code>

            .........


            GROUP BY crewID,long_posname
            ORDER BY pos_arrangement