Php 如何对两个日期之间过滤的数据求和?
我正在尝试将两个日期之间选定的数据相加,然后按部门进行分组。有人能帮我吗 我将在这里附加其余的代码。请帮忙。我真的需要把这件事做完。谢谢大家Php 如何对两个日期之间过滤的数据求和?,php,mysql,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_
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; }
?>
在未经验证的情况下使用$\u POST时要小心我假设您能够成功连接到数据库。我还使用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; }
?>
选择department,SUMtotal作为salary中的total_salary,其中date_time在$start_date和$end_数据组之间,按部门划分。您非常容易受到sql注入的影响,请注意这一点。查询前使用准备好的语句使用mysqli或pdo@SujeetAgrahari它给我这些错误mysqli_num_rows期望参数1是mysqli_result,第26行的布尔值这是我的第26行ifmysqli_num_rows$result>0脚本代码中的问题,而不是sql@RajendraSingh请参阅更新它给我这些错误mysqli_num_rows期望参数1是mysqli_result,第26行上的布尔值这是我的第26行如果mysqli_num_rows$result>0上述错误表示查询正在运行。您确定正在尝试上面的正确查询吗。您是否可以打印查询并尝试在phpMyAdmin中运行,以查看出现的错误。您是否尝试在phpMyAdmin中运行我的查询。当你说它没有错误时,是不是意味着它返回了0条记录。
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
$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; }
?>