PHP MYSQL在两个表之间使用日期完全连接

时间:2016-11-27 14:28:39

标签: php mysql

Hiii我有两个不同的表

  1. staff_rotas(id,staff_id,startDateTime,endDateTime,rota_minutes,status)
  2. staff_login(id,staff_id,actual_startDateTime,actual_endDateTime,shift_minutes,status);
  3. 我想从两个表中获取数据,例如(21-11-2016到27-11-2016)之间的日期并输出这样的内容 Timesheet

    我试过以下查询请检查。

    <?php 
    
    $staff_data = mysql_query("SELECT * FROM staff_rotas LEFT JOIN staff_login ON 
    (DATE(staff_rotas.startDateTime) = DATE(staff_login.actual_startDateTime))
     WHERE staff_rotas.staff_id = '123' AND DATE_FORMAT(staff_rotas.startDateTime,'%Y-%m-%d') 
     BETWEEN '" . $date_from . "' AND '" . $date_to . "'
     UNION 
     SELECT * FROM staff_rotas RIGHT JOIN staff_login ON (
     DATE(staff_login.actual_startDateTime) = DATE(staff_rotas.startDateTime)) 
     WHERE staff_login.staff_id = '123' AND 
     DATE_FORMAT(staff_login.actual_startDateTime,'%Y-%m-%d') BETWEEN '" . $date_from . "' AND '" . $date_to . "'
     ORDER BY `startDateTime` DESC")
    
    while($data = mysql_fetch_object($staff_data)){
        /*html table to show data ......
            ...
            ...
        */
    }
    
    ?>
    

0 个答案:

没有答案