如何对两个日期之间的过滤数据求和?

时间:2019-07-05 03:57:45

标签: php mysql

我正在尝试对两个日期之间的选定数据求和,然后按部门将其分组。有人可以帮帮我吗 :( 我将在此处附加其余代码。请帮忙。我真的需要完成这项工作。谢谢大家。

SELECT * FROM salary  
WHERE date BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."', Sum (total) as total_salary ] from 'salary' group by department";$result = mysqli_query($connect, $query);  
  $output .= '  
       <table> 
            <tr>  
                <th width="10%">Name</th>  
                <th width="2%">Position</th>  
                <th width="2%">Transaction ID</th>  
                <th width="3%">No of Days</th>  
                <th width="4%">Rate Per Day</th>  
                <th width="7%">Undertime</th>
                <th width="7%">Gross Pay</th>
                <th width="7%">Salary</th> 
            </tr>  
  ';  
  if(mysqli_num_rows($result) > 0)  
  {  
       while($row = mysqli_fetch_array($result))  
       {  
            $output .= '  
                 <tr>  
                      <td>'. $row["name"] .'</td>  
                      <td>'. $row["position"] .'</td>  
                      <td>'. $row["trasactionid"] .'</td>  
                      <td>'. $row["days"] .'</td>  
                      <td>'. $row["rate"] .'</td>  
                      <td>'. $row["undertime"] .'</td> 
                      <td>'. $row["gross"] .'</td> 
                      <td>'. $row["total"] .'</td> 
                 </tr>  
            ';  
       }  
  }  
  else  
  {  
       $output .= '  
            <tr>  
                 <td colspan="5">No Order Found</td>  
            </tr>  
       ';  
  }  
  $output .= '</table>';  
 echo $output;   }  
?>  

我期望这样的输出。用每个部门的总工资。

Department       Total_Salary
Maintenance      30,000

2 个答案:

答案 0 :(得分:0)

我假设您能够成功连接到数据库。我还使用prepare语句来防止sql注入。

$stmt = $mysqli->prepare("SELECT department, SUM(total) as total_salary FROM salary 
                          WHERE date_time BETWEEN ? AND ? group by department");
$stmt->bind_param("ss", $_POST["from_date"],$_POST["to_date"]);
$stmt->execute();
$result = $stmt->get_result();  
$output .= '  
   <table> 
        <tr>  
            <th width="10%">Name</th>  
            <th width="2%">Position</th>  
            <th width="2%">Transaction ID</th>  
            <th width="3%">No of Days</th>  
            <th width="4%">Rate Per Day</th>  
            <th width="7%">Undertime</th>
            <th width="7%">Gross Pay</th>
            <th width="7%">Salary</th> 
        </tr>  
  ';  
  if($result->num_rows > 0)  
 {  
   while($row = $result->fetch_assoc())  
   {  
        $output .= '  
             <tr>  
                  <td>'. $row["name"] .'</td>  
                  <td>'. $row["position"] .'</td>  
                  <td>'. $row["trasactionid"] .'</td>  
                  <td>'. $row["days"] .'</td>  
                  <td>'. $row["rate"] .'</td>  
                  <td>'. $row["undertime"] .'</td> 
                  <td>'. $row["gross"] .'</td> 
                  <td>'. $row["total"] .'</td> 
             </tr>  
        ';  
     }  
   }  
 else  
  {  
     $output .= '  
        <tr>  
             <td colspan="5">No Order Found</td>  
        </tr>  
   ';  
}  
  $output .= '</table>';  
  echo $output;   }  
   ?>  

答案 1 :(得分:-1)

SELECT sum(total) AS total_salary , department 
FROM salary 
WHERE date BETWEEN cast('".$_POST["from_date"]."' as Date) 
AND cast('".$_POST["to_date"]."' as Date) 
GROUP BY department

请谨慎使用未经验证的$ _POST