0

I have two tables one have employee name, employee id and another one is tblleaves have empid,Leave_Date, fromDate, toDate, Description.

If employee choose one date leave it stores the date value to Leave_Date and if employee choose multiple dates it store value of from date and to date.

The output page I want an employee name, Leave Days and Leave Dates. Leave Dates have dates from Leave_date, FromDate and 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);?>&nbsp;<?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>

The output i want:

  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)
Krishnan R
  • 45
  • 7
  • Help me to find the answer friends – Krishnan R Jun 09 '19 at 07:02
  • the mistake is in ur query, `tblemployees.id` and `tblemployees.EmpId` the same table name has second table column name. If you change this should work i think – Rasa Mohamed Jun 09 '19 at 09:08
  • I changed that I want to print the output in given – Krishnan R Jun 09 '19 at 09:11
  • Want fromdate and to date with dates see in the output I want help me to find it – Krishnan R Jun 09 '19 at 09:12
  • @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;` This query will give exact what you expect Name,Sum of all days by each employee, Cocat all leave taken dates – Rasa Mohamed Jun 09 '19 at 11:42
  • where should i give the month and year to search – Krishnan R Jun 09 '19 at 11:49
  • please guys give me an answer. – Krishnan R Jun 09 '19 at 11:54
  • Without learning basics you can't do anything, No One can feed you each and everything. `where YEAR(Leave_Date) = $Year AND MONTH(Leave_Date) = $Month AND DATEDIFF(tl.toDate,tl.fromDate)!='' GROUP BY employee_name;` – Rasa Mohamed Jun 09 '19 at 12:02
  • still no output bro – Krishnan R Jun 09 '19 at 12:05
  • Their is Leave_Date in my database if leave apply for one day it stores date – Krishnan R Jun 09 '19 at 12:08

1 Answers1

0

Can't help you with the php but the mysql query could do with work.

DROP TABLE IF EXISTS T;
CREATE TABLE T
(ID INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(10),LEAVE_DATE DATE, LEAVE_FROM DATE, LEAVE_TO DATE);
INSERT INTO T (NAME,LEAVE_DATE,LEAVE_FROM,LEAVE_TO) VALUES
('A','2019-05-30','2019-05-30','2019-06-02'),
('A','2019-06-05',NULL,NULL),
('A','2019-06-06',NULL,NULL),
('A','2019-06-30','2019-06-30','2019-07-11'),
('B','2019-05-30','2019-05-30','2019-07-11'),
('C','2019-05-11','2019-05-11','2019-05-12')
;

SELECT NAME,
         SUM(CASE WHEN LEAVE_FROM IS NULL THEN 1 
                ELSE DATEDIFF(IF(LEAVE_TO > '2019-06-30','2019-06-30',LEAVE_TO),IF(LEAVE_FROM < '2019-06-01','2019-06-01',LEAVE_FROM)) + 1 
              END) LEAVEDAYS,
         GROUP_CONCAT(CASE WHEN LEAVE_FROM IS NULL THEN LEAVE_DATE 
                            ELSE CONCAT(IF(LEAVE_FROM < '2019-06-01','<<2019-06-01',LEAVE_FROM),' & ',IF(LEAVE_TO > '2019-06-30','2019-06-30>>',LEAVE_TO)) 
                            END) AS LEAVEDATES 
FROM T
WHERE (LEAVE_DATE BETWEEN '2019-06-01' AND '2019-06-30') OR
        (LEAVE_FROM BETWEEN '2019-06-01' AND '2019-06-30') OR
        (LEAVE_TO   BETWEEN '2019-06-01' AND '2019-06-30') OR
        (LEAVE_FROM < '2019-06-01' AND LEAVE_TO > '2019-06-30')
GROUP BY NAME
ORDER BY ID;

Note the adjustments to top and bottom the leave dates to ensure we are only counting dates in the period. Note also that I have added indicators(<< or >>) to the leave dates to indicate leave dates which begin before the period start date required or extend after the period end date required.

+------+-----------+---------------------------------------------------------------------------+
| NAME | LEAVEDAYS | LEAVEDATES                                                                |
+------+-----------+---------------------------------------------------------------------------+
| A    |         5 | <<2019-06-01 & 2019-06-02,2019-06-05,2019-06-06,2019-06-30 & 2019-06-30>> |
| B    |        30 | <<2019-06-01 & 2019-06-30>>                                               |
+------+-----------+---------------------------------------------------------------------------+
2 rows in set (0.08 sec)
P.Salmon
  • 17,104
  • 2
  • 12
  • 19
  • I have empid with employee details in another table – Krishnan R Jun 09 '19 at 07:45
  • SELECT SUM(CASE WHEN FromDateISNULLTHEN1ELSEDATEDIFF(IF(ToDate > '2019-06-30','2019-06-30',ToDate),IF(FromDate< '2019-06-01','2019-06-01',FromDate))+1END)LEAVEDAYS,GROUP_CONCAT(CASE WHEN FromDate IS NULL THEN Leave_Date ELSE CONCAT(IF(FromDate< '2019-06-01','<<2019-06-01',FromDate),' & ',IF(ToDate > '2019-06-30','2019-06-30>>',ToDate)) END) AS LEAVEDATES FROM tblleaves WHERE (Leave_Date BETWEEN '2019-06-01' AND '2019-06-30') OR (FromDate BETWEEN '2019-06-01' AND '2019-06-30') OR (ToDate BETWEEN '2019-06-01' AND '2019-06-30') OR (FromDate < '2019-06-01' AND ToDate > '2019-06-30') GROUP BY empid – Krishnan R Jun 09 '19 at 07:57
  • I tried this code But output is LEAVEDAYS LEAVEDATES -2 <<2019-06-01 & ,<<2019-06-01 & ,2019-06-09 & 2019-... NULL <<2019-06-01 & – Krishnan R Jun 09 '19 at 07:58
  • Help me to find it – Krishnan R Jun 09 '19 at 07:58
  • Seriously you think I'm going to download from an untrusted source? – P.Salmon Jun 09 '19 at 08:54
  • it just php file and sql file for your reference – Krishnan R Jun 09 '19 at 08:56
  • Help me to find out bro – Krishnan R Jun 09 '19 at 08:56