0

我有一个看起来像这样的数据库:

Employee_id  dev_hours Pm_hours
1                2         2
1                3         3
2                3         3
3                2         1 
3                1         1

我将如何使用 php 来查找每个员工的总工作时间?例如,员工 1 总共工作了 10 个小时,而员工 3 总共工作了 5 个小时?

4

2 回答 2

3

您需要使用SUM()GROUP BY。尝试:

SELECT Employee_id, SUM(dev_hours + Pm_hours) as total_hours FROM tbl GROUP BY Employee_id
于 2013-02-20T22:54:59.480 回答
0

此处取自 w3 学校 PHP 指南的细微变化:

http://www.w3schools.com/php/php_mysql_select.asp

<?php
$con = mysql_connect("server","user","password");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT Employee_id, SUM(dev_hours + Pm_hours) as Hours FROM EmployeeTable GROUP BY Employee_id");

while($row = mysql_fetch_array($result))
{
  echo $row['Employee_id'] . " " . $row['Hours'];
  echo "<br />";
}

mysql_close($con);
?>
于 2013-02-20T22:59:15.987 回答