如何按某个字段值分组并获得唯一记录

时间:2019-03-02 12:45:25

标签: php codeigniter

您好,我有一个称为部门的表格,我将根据出勤情况显示报告,我需要将部门中的所有雇主合并到各自的部门。例如:部门1 => 1,2,3,部门2 => 4,5,6。我尝试了groupby,但是合并了具有某个部门的所有雇主,即当我使用groupby时,部门1仅包含3。 php中是否有函数在获取时获取唯一数据

[0] => Array
        (
            [department_id] => 15
            [department_name] => R&D
            [name] => SUBRAMANIAN
            [code] => 101
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [1] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => RAVI
            [code] => 103
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [2] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => AMULRAJAN.A
            [code] => 104
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [3] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => KANAKARAJ.R
            [code] => 105
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [4] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => ELAYARAJA
            [code] => 106
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [5] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => SIVAKUMAR.T
            [code] => 108
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [6] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => PALANISAMY.R
            [code] => 110
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [7] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => SAKKIYAN.K
            [code] => 112
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [8] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => Jeyaprakash
            [code] => 113
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

我需要部门->制造包含RAVI,AMULRAJAN.A,KANAKARAJ.R等,在使用groupby时无法合并

这是我的mysql查询:

SELECT `tbl_employee_basicdetails`.`employee_id`, `tbl_employee_basicdetails`.`employee_name`, `tbl_employee_basicdetails`.`employee_code`,  `tbl_employee_officedetails`.`department` FROM `tbl_employee_basicdetails` JOIN `tbl_employee_officedetails` ON `tbl_employee_officedetails`.`employee_id`=`tbl_employee_basicdetails`.`employee_id` WHERE `tbl_employee_basicdetails`.`employee_act` = 1

1 个答案:

答案 0 :(得分:0)

假设一个sql结果数组,php中的一种解决方案是将结果数组“转换”为一个以department_name为键,而雇员详细信息为元素的数组。这样的事情应该起作用:

$departments=[];
foreach ($results as $emp) {

    $departments[$emp['department_name']][] = $emp;
}