显示具有相同表但不同条件sql的数据

时间:2018-01-25 05:23:42

标签: sql

enter image description here

我有这样的结果,缺席当前wolcha是好的,但我想通过获取或只显示以前的wolcha总数不需要缺席,lates等只是总数来获得wolcha的先前状态。

我有这样的结果,缺席当前wolcha是好的,但我想通过获取或只显示以前的wolcha总数不需要缺席,lates等只是总数来获得wolcha的先前状态。

function getwolchastatus($salon_id){
        global $db;
        $query = "SELECT employee_attendance.emp_id,
        employee_attendance.date,
        employee_attendance.branch_id,
        employee_attendance.user_id,
        employee_profile.emp_no,
        COUNT(IF(employee_attendance.status='A',1, NULL)) 'absent',
        COUNT(IF(employee_attendance.status='L',1, NULL)) 'late',
        COUNT(IF(employee_attendance.status='SL',1, NULL)) 'sick_leave',
        COUNT(IF(employee_attendance.status='LWP',1, NULL)) 'leave_w_pay',
        COUNT(IF(employee_attendance.status='LWOP',1, NULL)) 'leave_wo_pay',
        COUNT(IF(employee_attendance.status='HD',1, NULL)) 'halfday'      
        FROM employee_attendance
        INNER JOIN employee_profile ON employee_profile.emp_id = employee_attendance.emp_id
        INNER JOIN users ON users.user_id = employee_profile.user_id WHERE MONTH(employee_attendance.date) = MONTH(CURRENT_DATE) AND YEAR(employee_attendance.date) = YEAR(CURRENT_DATE) AND users.status = 'activate' AND users.salon_id = 2 GROUP BY employee_attendance.emp_id";
        $result = $db->query($query) or die($db->error);
        $content = '';
            while($row = $result->fetch_array()) { 
                extract($row);
                setlocale(LC_MONETARY, 'en_PH');

        $query2 = "SELECT employee_attendance.emp_id,
        employee_attendance.date,
        employee_attendance.branch_id,
        employee_attendance.user_id,
        employee_profile.emp_no,
        COUNT(IF(employee_attendance.status='A',1, NULL)) 'absent2',
        COUNT(IF(employee_attendance.status='L',1, NULL)) 'late2',
        COUNT(IF(employee_attendance.status='SL',1, NULL)) 'sick_leave2',
        COUNT(IF(employee_attendance.status='LWP',1, NULL)) 'leave_w_pay2',
        COUNT(IF(employee_attendance.status='LWOP',1, NULL)) 'leave_wo_pay2',
        COUNT(IF(employee_attendance.status='HD',1, NULL)) 'halfday2'      
        FROM employee_attendance
        INNER JOIN employee_profile ON employee_profile.emp_id = employee_attendance.emp_id
        INNER JOIN users ON users.user_id = employee_profile.user_id WHERE employee_attendance.date between '2017-12-01' and '2017-12-31' AND users.status = 'activate' AND users.salon_id = 2 GROUP BY employee_attendance.emp_id";
        $result2 = $db->query($query2) or die($db->error);
        $row2 = $result2->fetch_array();

            $new_branch = new Branch;
            $employee = new Employee;
            $new_user = new Users;
            $branch_name = $new_branch->get_branch_info($row['branch_id'], 'branch_name');
            $first_name = $employee->get_employee_info($row['emp_id'], 'first_name');
            $last_name = $employee->get_employee_info($row['emp_id'], 'last_name');
            $nickname = $employee->get_employee_info($row['emp_id'], 'nickname');
            $employee_name = $nickname; 
            //$today = date('MONTH');
            // $date1 = DATEADD('DAY', -30, GETDATE());


                if(($row['absent'] <3) && ($row['late'] < 2) &&($row['sick_leave'] < 1) && ($row['leave_w_pay'] < 1) && ($row['leave_wo_pay'] < 1) && ($row['halfday'] <2)){
                    $row['total'] = 1;
                }else{
                    $row['total'] = 0;
                }

                if(($row2['absent2'] <3) && ($row2['late2'] < 2) &&($row2['sick_leave2'] < 1) && ($row2['leave_w_pay2'] < 1) && ($row2['leave_wo_pay2'] < 1) && ($row2['halfday2'] <2)){
                    $row2['total2'] = 1;
                }else{
                    $row2['total2'] = 0;
                }

            $total3 = $row['total'] + $row2['total2'];

        $content .= "<tr>";
        // $content .= "<td>";
        // $content .= $branch_name;
        // $content .= "</td>";
        $content .= "<td>";
        $content .= $last_name.' '.$first_name;
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row['absent'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row['late'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row['sick_leave'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row['leave_w_pay'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row['leave_wo_pay'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row['halfday'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row['total'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $row2['total2'];
        $content .= "</td>";
        $content .= "<td>";
        $content .= $total3;
        $content .= "</td>";
        $content .= "</tr>";
        }
        echo $content;
    }

0 个答案:

没有答案