【发布时间】:2019-06-09 05:50:16
【问题描述】:
我有两张表,一张有员工姓名、员工 id,另一张是 tblleaves,有 empid、Leave_Date、fromDate、toDate、Description。
如果员工选择一个日期休假,则将日期值存储到 Leave_Date,如果员工选择多个日期,则存储从日期到至今的值。
我想要员工姓名、休假天数和休假日期的输出页面。 Leave Dates 的日期为 Leave_date、FromDate 和 ToDate。
<?php
if(isset($_POST['apply'])){
$ym=$_POST['month'];
list($Year, $Month) = explode("-", "$ym", 2);
$sql = "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) = $Year
AND MONTH(Leave_Date) = $Month
GROUP BY tblemployees.EmpId";
$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);?> <?php echo htmlentities($result->LastName);?></td>
<td><?php echo htmlentities($result->Leave_Days);
?></td>
<td><?php echo htmlentities($result->leave_dates);
?></td><?php $cnt++;}}}?>
</tr>
</tbody>
</table>
我想要的输出:
employee name Leave Days Leave Dates
KrishnanR 3 12-06-2019, 13-06-2019, 14-06-2019
(FromDate and ToDate)
PrakashR 1 12-06-2019
(Leave_Date)
SelvaK 3 12-06-2019,13-06-2019&14-06-2019|14-06-2019
(FromDate and ToDate) (Leave_Date)
【问题讨论】:
-
帮我找答案的朋友
-
错误在你的查询中,
tblemployees.id和tblemployees.EmpId同一个表名有第二个表列名。如果你改变这应该工作我认为 -
我改变了我想打印给定的输出
-
想要从日期到日期的日期在输出中看到我想帮我找到它
-
@Krishnan R
SELECT CONCAT(te.FirstName, te.LastName) as employee_name, SUM(DATEDIFF(tl.toDate,tl.fromDate)+IF(DATEDIFF(tl.toDate,tl.fromDate)!='',1,0)) as Leave_Days, GROUP_CONCAT(tl.fromDate,',',tl.toDate) as Leave_Dates from tblemployees te join tblleaves tl on tl.empid = te.id where DATEDIFF(tl.toDate,tl.fromDate)!='' GROUP BY employee_name;此查询将准确给出您的期望姓名、每位员工所有天数的总和、Cocat 所有休假日期