gpt4 book ai didi

php - MySQL 和 PHP : summing up data from a table

转载 作者:行者123 更新时间:2023-11-29 05:36:24 25 4
gpt4 key购买 nike

好吧,这可能有一个简单的答案,但现在已经困扰我几个小时了。

我正在使用 PHP/HTML 从 MySQL 表生成一个表。在 MySQL 表 (TimeRecords) 中,我有一个 StartTime 和 EndTime 列。在我的 SELECT 语句中,我从 StartTime 中减去 EndTime,并将其别名为 TotalHours。到目前为止,这是我的查询:

$query = "SELECT *,((EndTime - StartTime)/3600) AS TotalPeriodHours
FROM TimeRecords
WHERE Date
BETWEEN '{$CurrentYear}-{$CurrentMonth}-1'
AND '{$CurrentYear}-{$CurrentMonth}-31'
ORDER BY Date
";

然后我通过 HTML 表格循环它。到目前为止,一切都很好。我想做的是将所有 TotalHours 相加并将其放入单独的 DIV 中。关于 1) 如何编写 select 语句和 2) 从 PHP/HTML 调用该代码的位置有什么想法吗?

提前致谢!

最佳答案

试试这个

$query= "
SELECT ((EndTime - StartTime)/3600) AS Hours, otherFields, ...
FROM TimeRecords
WHERE
Date BETWEEN '{$CurrentYear} - {$CurrentMonth} - 1'
AND '{$CurrentYear}-{$CurrentMonth} - 31' ";

$records =mysql_query($query);
$sum= 0;
while($row=mysql_fetch_array($records))
{
echo"$row['otherFields']";
echo"$row['Hours']";
$sum+=$row['Hours'];
}

echo" Total Hours : $sum ";

关于php - MySQL 和 PHP : summing up data from a table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10184924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com