员工月休假报告

时间:2019-06-08 12:07:23

标签: php mysql

我有两个表,一个表具有员工姓名,雇员ID,另一个表tblleaves具有Empid,Leave_Date,fromDate,toDate,Description。

如果用户选择一天休假,则将日期值存储到Leave_Date;如果用户选择多休假,则将from和date的值存储到日期。

现在我想要员工的月度报告。在此页面中,我需要一个员工姓名,请假天数和请假日期。我尝试了代码,但我反复得到了雇员的姓名,因为他们在该月申请了很多休假,所以我想一次显示雇员的姓名。

<?php 


    if(isset($_POST['apply'])){

    $ym=$_POST['month'];
    list($Year, $Month) = explode("-", "$ym", 2);



     $sql = "SELECT distinct tblleaves.id as lid,tblemployees.FirstName,tblemployees.LastName,tblemployees.EmpId,
tblemployees.id,tblleaves.LeaveType,tblleaves.PostingDate,
tblleaves.Leave_Date from tblleaves join tblemployees on tblleaves.empid=tblemployees.id 
WHERE YEAR(Leave_Date) = 2019 AND MONTH(Leave_Date) = 6";
    echo $sql;
    $query = $dbh -> prepare($sql);
    $query->execute();
    $results=$query->fetchAll(PDO::FETCH_OBJ);

    $cnt=1;
    if($query->rowCount() > 0)
    {
    foreach($results as $result)
    {               ?>  
                                            <tr>
                                                <td> <?php echo htmlentities($cnt);?></td>



       <td><?php echo htmlentities($result->FirstName);?>&nbsp;<?php echo htmlentities($result->LastName);?></td>
                                               <td><?php ?></td>
                                             <td><?php $result->Leave_Date?></td>
                                             <?php $cnt++;}}}?>

我想要员工月休假报告

employee name     Leave Days      Leave Dates 
KrishnanR            3              12-06-2019, 13-06-2019, 14-06-2019

1 个答案:

答案 0 :(得分:0)

实际上,应在报表本身中进行这种格式化,方法是让每个员工都有一个带,并显示名称,然后在带内显示该员工的所有记录。

但是,您可以获得使用MySQL函数GROUP_CONCAT()发布包含示例结果的数据集。

以下代码将为您提供三列,名字,姓氏和请假日期,每位员工一行。 Leave_date列将包含所有它们的tblleaves.Leave_Date值,并用逗号和空格(或在关键字SEPARATOR之后出现的所有内容分隔。)

您不需要DISTINCT

SELECT 
   tblemployees.FirstName,
   tblemployees.LastName,
   count(tblleaves.empid) as Leave_Days,
   GROUP_CONCAT( tblleaves.Leave_Date SEPARATOR ', ' ) AS leave_dates
FROM
   tblleaves
   JOIN tblemployees
      ON tblleaves.empid = tblemployees.id
WHERE YEAR(Leave_Date) = 2019
   AND MONTH(Leave_Date) = 6
GROUP BY tblemployees.EmpId