Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP在while循环中求和一个值,但带有条件_Php_Loops_While Loop_Sum - Fatal编程技术网

PHP在while循环中求和一个值,但带有条件

PHP在while循环中求和一个值,但带有条件,php,loops,while-loop,sum,Php,Loops,While Loop,Sum,我有两个表要加入,一个是用户,一个是出勤 TABLE : attendance id userId totalHours 1 1 0745 2 3 0845 3 1 0945 TABLE : user id name departmentId 1 John 2 2 Sean 2 3 Allan 2 并非每个用户都有出勤记录(总小时数) 但是我需要按userId查询,其中departmentId=

我有两个表要加入,一个是用户,一个是出勤

TABLE : attendance
id   userId   totalHours 
1    1        0745
2    3        0845
3    1        0945

TABLE : user
id   name  departmentId
1    John  2
2    Sean  2
3    Allan 2
并非每个用户都有出勤记录(总小时数) 但是我需要按userId查询,其中departmentId=XXXX,并对每个存在的总小时数求和,而不忽略没有任何出勤记录的userId

TABLE : attendance
id   userId   totalHours 
1    1        0745
2    3        0845
3    1        0945

TABLE : user
id   name  departmentId
1    John  2
2    Sean  2
3    Allan 2
到目前为止,我做了以下几点:

$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
  $id = $row['userId'];
  $result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
  while($row2 = mysqli_fetch_array($result2))
  $totalHours = 0;
  {
     $totalHours = $row2['totalHours'];
     $grandTotal += $totalHours;
     $totalHoursInHHmm = substr_replace($totalHours,":",2,0); 
     $parsed = date_parse($totalHoursInHHmm); 
     $toSeconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60;
     $total += $toSeconds;
     $init = $total;
     $hours = floor($init / 3600);
     $minutes = floor(($init / 60) % 60);    
  }
  echo "$hours:$minutes";
}
结果显示了部门中的所有用户,并对每个userId的所有总小时数进行了合计,但问题是,有个userId没有任何出勤,仍然显示了SUM值,继承了以前的总和

感谢您的帮助:)

我需要通过userId进行查询,其中departmentId=XXXX,并对每个 它们存在的总小时数,而不忽略用户ID,没有任何 记录出席人数

TABLE : attendance
id   userId   totalHours 
1    1        0745
2    3        0845
3    1        0945

TABLE : user
id   name  departmentId
1    John  2
2    Sean  2
3    Allan 2
要显示给定部门中所有用户的小时数,甚至是
考勤
表中没有行的用户,请使用
左联接

使用
(强制转换(totalHours作为未签名)%100)/60+楼层(强制转换(totalHours作为未签名)/100)
将您的varchar小时+分钟转换为单个小时数

$query = "SELECT u.id, 
SUM((CAST(totalHours AS UNSIGNED) % 100)/60 + FLOOR(CAST(totalHours AS UNSIGNED)/100)) grandTotal
FROM user u
LEFT JOIN attendance a
ON u.id = a.userId
WHERE u.departmentId = 2
GROUP BY u.id";

$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result)) {
    print $row['id'] . ' ' . $row['grandTotal'];
}

试试这个,就在第一个,而你不会同时需要这两个

SELECT TIME_FORMAT(sum(STR_TO_DATE(a.totalHours, '%i')),'%H:%i') as sum, u.id, u.name FROM user AS u LEFT JOIN attendance AS a ON a.userId = u.id WHERE u.departmentId = 2 AND u.id = $user_id GROUP by u.id;
更新,尝试,不确定它是否会工作,我不能测试它现在,但参考这个问题


一旦您得到了正确的查询,在php中完成其余的工作将非常容易。DB应该做这项工作,尽管这里的模式并不理想。

我从问题中了解到的是,即使没有附加记录,也不要忽略这些用户ID。在这个场景中,我有两个选择

1.

$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
  $id = $row['userId'];
  $result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
  $grandTotal=0;
  while($row2 = mysqli_fetch_array($result2))
  $totalHours = 0;
  {
     $totalHours = $row2['totalHours'];
     $grandTotal += $totalHours      
  }
  echo $grandTotal;
}
$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
  $id = $row['userId'];
  $result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
  while($row2 = mysqli_fetch_array($result2))
  $totalHours = 0;
  {
     $totalHours = $row2['totalHours'];
     if($totalHours<=0)
    $grandTotal=0;
     $grandTotal += $totalHours      
  }
  echo $grandTotal;
}
2.

$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
  $id = $row['userId'];
  $result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
  $grandTotal=0;
  while($row2 = mysqli_fetch_array($result2))
  $totalHours = 0;
  {
     $totalHours = $row2['totalHours'];
     $grandTotal += $totalHours      
  }
  echo $grandTotal;
}
$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
  $id = $row['userId'];
  $result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
  while($row2 = mysqli_fetch_array($result2))
  $totalHours = 0;
  {
     $totalHours = $row2['totalHours'];
     if($totalHours<=0)
    $grandTotal=0;
     $grandTotal += $totalHours      
  }
  echo $grandTotal;
}
$result=mysqli_query($con,“从departmentId=2的用户中选择*);
while($row=mysqli\u fetch\u数组($result))
{
$id=$row['userId'];
$result2=mysqli_查询($con,“从考勤中选择*,其中userId=$id”);
while($row2=mysqli\u fetch\u数组($result2))
$totalHours=0;
{
$totalHours=$row2['totalHours'];

如果($totalHoursOK!发生这种情况的原因是,没有任何出席的用户没有通过第二个时间段,那么这些值不会被重新启动。您只需在回显后设置$grandTotal即可更正此问题。如下所示:

$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
  $id = $row['userId'];
  $result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
  while($row2 = mysqli_fetch_array($result2))
  {
     $totalHours = 0;
     $totalHours = $row2['totalHours'];
     $grandTotal += $totalHours      
  }
  echo $grandTotal;
  $grandTotal = 0;
}
  • 在大括号{}内移动$totalhours=0
  • 在第二个while循环的顶部设置$hours=$minutes=0(其中设置$totalhours=0) **如果不重置$hours和$minutes,则没有出勤的用户将获得旧值


你能发布相关表格的架构吗?你可能想通过和sum()进行联接或左联接分组。也许,用户表格和考勤表的关系如何?好的,我会编辑,请帮助:锁定第二个while循环,我想你的意思是使用$totalHours=$row2['totalHours']在第一个while循环开始之前,我会将$grandTotal设置为0。在第二个while循环开始之前,请尝试$grandTotal=0。OP询问如何将未参加的用户排除在统计范围之外,即,他需要将用户表加入考勤表,以获得正确的结果集。OP只需要那些总小时数的用户。“但问题是,存在没有任何出席的userId"@ArtisiticPhoenix
不忽略没有任何出席记录的用户id
表示他也想要没有总小时的用户。你说的对,问题是令人困惑的,我们都同意OP最好是使用一个带总和的连接和组。虽然他现在发布了另一个表,但u.id不是u.userId。这就是诀窍!谢谢FuzzyTree.你的帮助让我很开心,我一直被我糟糕的数据库规划所困扰。感谢所有帮助我的人,希望我能报答所有人的好意和花费的时间。我是ArtisticPhoenix,我不能总结()由于数据是不带冒号的时间格式的列,我需要在循环中求和,因为我需要将其转换为秒并从那里添加。你知道吗?你仍然可以求和。你必须将前两位数字转换为小时,然后将其转换为分钟,再添加到后两位,然后求和。这可能有效。该行的字段类型是什么?Artisitic,在第二个while循环中是varchar(50),他应该使用$row2,而不是$row。这将无法正常工作。“$totalHours=0”是while循环的主体,因为它放错了位置。我没有注意到“$totalHours=0”超出了while循环的范围。他必须在其中移动它。我已经纠正了我的示例,包括“$row2 VS.$row”问题。第二个while循环不起作用。正文是“$totalHours=0”,因为它位于循环声明和预期正文之间。@Peter我认为相同,但than OP有相同的代码,所以我为他写了相同的代码……但我同意u:)尝试但返回了0:(我确信我无法理解您的问题,因为…bcoz我希望它返回0,它正在返回该值。