作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,这可能有一个简单的答案,但现在已经困扰我几个小时了。
我正在使用 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/
我是一名优秀的程序员,十分优秀!