显示范围之间每个日期的记录

时间:2012-07-20 09:03:43

标签: php mysql

我想显示数据库中每个日期的记录,以显示用户提供的日期范围。 即如果用户指定2012年7月2日至2012年7月4日。 然后 输出必须是

date:2012-july-02  
name | intime | outtime |Teamtype

date:2012-july-03
name | intime | outtime |Teamtype  

so on..

SQL for this:

 $s="SELECT InDate,mstEmp.cardnofrom mstEmp left join dailyattendance ON (InDate BETWEEN '$from' AND '$to' ) AND(dailyattendance.HolderName = mstEmp.empname ) where teamtype='$chk'";
$objDB->SetQuery($s);
$r = $objDB->GetQueryReference();

 $sql= SELECT mstEmp.empname,   
       mstEmp.cardno, 
       InDate, 
       dailyattendance.InTime,
       dailyattendance.OutTime, 
       mstEmp.teamtype            
FROM mstEmp  
 LEFT JOIN dailyattendance    
 ON (InDate BETWEEN '$from' AND '$to')  AND (dailyattendance.HolderName =mstEmp.empname )   
WHERE mstEmp.teamtype = '$chk' 
ORDER BY InDate DESC
$res = $objDB->GetQueryReference();

我尝试的是

  <!-- language: lang-php -->

    while($rw=mysql_fetch_array($r) )
  {

    while($row=mysql_fetch_array($res))
    {
        list($name, $crd,$indate,$in,$out,$t) = $row;
        if($rw[1]!=$crd && $row[2]=='')
        {
            $d1='NULL';
            $d2='NULL';
        }
        else
        {
        $d1 = date("g:i A", $in);
        $d2 = date("g:i A" ,$out);
        $d3 = date("d-m-Y (D)", $in);
        $bg = '';
        if(date('D', $in) == 'Sun' || date('D', $in) == 'Sat')
        $bg = 'bgcolor=#EEEEEE';
        else 
        $bg = 'bgcolor= #ECE5B6';
        if ($ddate != $row[2])
        {   
            echo "<tr $bg><td colspan=4>Date: ".$d3."</td></tr>";
            $ddate = $row[2];
        }
        }

        echo "<tr>";
        echo "<td align=left width='35%'>".trim($name)."</td>";
        echo "<td align=center width='20%' >".$d1."</td>";
        echo "<td align=center  width='23%' >".$d2."</td>";
        echo "<td align=center width='16%'>".$t."</td>";
        echo "</tr>";
    }   
  echo "</table><br>";
  echo "</form>";
  }

mysql_free_result($res);
mysql_free_result($r);

1 个答案:

答案 0 :(得分:0)

您过滤天数应该属于WHERE而不是ON

SELECT
 mstEmp.empname,
 mstEmp.cardno,
 InDate,
 dailyattendance.InTime,
 dailyattendance.OutTime,
 mstEmp.teamtype
FROM mstEmp
LEFT JOIN dailyattendance
  ON (dailyattendance.HolderName = mstEmp.empname )
WHERE
  mstEmp.teamtype = '$chk' AND
  InDate BETWEEN '$from' AND '$to'
ORDER BY
  InDate DESC