每月显示休假记录

时间:2014-08-20 19:36:18

标签: php mysql

我有来自每个行的数据库的记录。我想要的是每个月显示每个用户的叶子记录。

我要展示的内容

Month      Annual Casual Medical Other
Jul 2014      4      2      -     -
Aug 2014      -      -      -     2
Sep 2014      1      3      1     -

我的结果是什么

Month      Annual Casual Medical Other
Jul 2014      4      -      -     -
Aug 2014      -      -      -     2
Sep 2014      1      3      1     -
Jul 2014      -      2      -     -

我不希望这个月重复如我所说的结果。如果在7月份有年度,休闲或任何其他类型的假期,则该月份不应重复,并且记录应以我上面提到的方式显示。

我的SQL查询是:

SELECT `leaves`.id,
       `leaves`.emp_id,
       employee.employeeCode,
       employee.employeeName,
       employee.designation,
       department.`name` as department,
       employee.email, 
       employee.annualLeave,
       employee.casualLeave,
       employee.medicalLeave,
       employee.carryForward,
       YEAR(dateFrom) as YEAR,
       DATE_FORMAT(dateFrom, '%b') as MONTH,
       SUM(`leaves`.noOfDays) as totalNoLeaves ,
       `leaves`.type FROM 
       employee 
       INNER JOIN department ON (employee.deptt_id = department.id ) 
     LEFT JOIN `leaves` ON (employee.id = `leaves`.emp_id) 
 WHERE employee.id = '1' GROUP BY `leaves`.type, DATE_FORMAT(`leaves`.dateFrom, '%b') ORDER BY employee.id ASC

我的PHP代码是:

$data= array();
while($result  = mysql_fetch_assoc($query)){
$data[] = $result;
}

<table border="1" width="500" class="listing">
<tbody>
<tr>
<th class="record-view-label"><strong>Month</strong></th>
<td class="record-view-value"><strong><div align="center">Annual</div></strong></td>
<td class="record-view-value"><strong><div align="center">Casual</div></strong></td>
<td class="record-view-value"><strong><div align="center">Medical</div></strong></td>
<td class="record-view-value"><strong><div align="center">Other</div></strong></td>
</tr>
<?php 
foreach($data as $value){
if ($value['type'] == "Annual"){ $annual = $value['totalNoLeaves'];}else {$annual = "-";}
if ($value['type'] == "Casual"){ $casual = $value['totalNoLeaves'];}else {$casual = "-";}
if ($value['type'] == "Medical"){ $medical = $value['totalNoLeaves'];}else {$medical = "-";}
if ($value['type'] == "Other"){ $oth = $value['totalNoLeaves'];}else {$oth = "-";}
?>
<tr>
<th class="record-view-label"><?=$value['MONTH'].' - '.$value['YEAR'];?></th> 
<td class="record-view-value"><div align="center"><?=$annual;?></div> </td>
<td class="record-view-value"><div align="center"><?=$casual;?></div> </td>
<td class="record-view-value"><div align="center"><?=$medical;?></div> </td>
<td class="record-view-value"><div align="center"><?=$oth;?></div> </td>
</tr>
<?php }?>
</table>

1 个答案:

答案 0 :(得分:0)

请使用字段值作为列名,然后您可以轻松解决此问题。 使用以下查询:

SELECT leaves。id,SUM(CASE WHEN(leaves。type ='Annual')THEN leaves。noOfDays ELSE NULL END)AS Annual,SUM(CASE WHEN({ {1}} .type ='Casual')THEN leaves。noOfDays ELSE NULL END)AS Casual,SUM(CASE WHEN(leaves。type ='Medical' )THEN leaves。noOfDays ELSE NULL END)AS Medical,SUM(CASE WHEN(leaves。type ='Other')THEN leaves。noOfDays ELSE NULL END)AS Other, leaves。emp_id,employee.employeeCode,employee.employeeName,employee.designation,department。leaves AS部门,employee.email,employee.annualLeave,employee.casualLeave,employee.medicalLeave,employee.carryForward, YEAR(dateFrom)AS YEAR,DATE_FORMAT(dateFrom,'%b')AS MONTH,SUM(name。noOfDays)AS totalNoLeaves 来自员工 INNER JOIN部门ON(employee.deptt_id = department.id) LEFT JOIN leaves ON(employee.id = leaves。emp_id) WHERE employee.id ='1' GROUP BY DATE_FORMAT(leaves。dateFrom,'%b') ORDER BY employee.id ASC