gpt4 book ai didi

php - 无法获取sql中所有相似行的总和

转载 作者:行者123 更新时间:2023-11-29 06:28:39 24 4
gpt4 key购买 nike

我有一个学生的预订表,其中列出了他预订的所有老师。结构如下:

Teacher Name    Date     start_time   end_time     hour       rate

现在我想列出预订如下,我想计算学生向每位老师支付的总费用以及为每位老师预订的总小时数。我想用一个查询来完成它,我正在使用 php 和 mysql。

 S. No  Teacher's Name         Date      Class Start time    Class End Time   Hours    Rate

1. Amit Kumar 5-10-2019 8:00 A.M. 9:00 A.M. 1 2000.00
2. Amit Kumar 6-10-2019 9:00 A.M. 10:00 A.M. 1 5000.00
Total 2 7000.00

3. Pawan Kumar 7-10-2019 10:00 A.M. 11:00 A.M. 1 4000.00
4. Pawan Kumar 8-10-2019 7:00 A.M. 8:00 A.M. 1 4000.00
Total 2 8000.00
...
...

最佳答案

SELECT TeacherName, SUM(Hours), SUM(Rate) FROM TABLE GROUP BY TeacherName

我认为你不能查询显示预订类(class)的详细部分,除非你要在 html 部分添加一些功能。

编辑---------------------

试试这个

<?php
$conn = new PDO('mysql:host=localhost', 'root', '');

$sel = $conn->query("SELECT * FROM db_try.TeachersBooking");

$cnt = 0;
$TeacherName = "";
$arrData = array();
while ($row = $sel->fetch(PDO::FETCH_ASSOC)) {
if($TeacherName != $row['TeacherName'])
{
$cnt = 0;
}

$TeacherName = $row['TeacherName'];
$arrData[$TeacherName][$cnt]['TeacherName'] = $row['TeacherName'];
$arrData[$TeacherName][$cnt]['Date'] = $row['Date'];
$arrData[$TeacherName][$cnt]['start_time'] = $row['start_time'];
$arrData[$TeacherName][$cnt]['end_time'] = $row['end_time'];
$arrData[$TeacherName][$cnt]['hour'] = $row['hour'];
$arrData[$TeacherName][$cnt]['rate'] = $row['rate'];

$cnt++;
}
?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table cellspacing="20">
<tr>
<th>No</th>
<th>Teacher's Name</th>
<th>Date</th>
<th>Class start time</th>
<th>Class end time</th>
<th>Hours</th>
<th>Rate</th>
</tr>
<?php
$count = 1;
foreach ($arrData as $key => $Teacher) {
$TeacherRate = $TeacherHours = 0;

foreach ($Teacher as $key => $row) {
echo "<tr>
<td>".$count."</td>
<td>".$row['TeacherName']."</td>
<td>".$row['Date']."</td>
<td>".$row['start_time']."</td>
<td>".$row['end_time']."</td>
<td>".$row['hour']."</td>
<td>".$row['rate']."</td>
</tr>";

$TeacherHours += $row['hour'];
$TeacherRate += $row['rate'];
$count++;
}

echo "<tr>
<td colspan='5'>Total</td>
<td>".$TeacherHours."</td>
<td>".$TeacherRate."</td>
</tr>";
}

?>
</table>
</body>
</html>

结果:

enter image description here

关于php - 无法获取sql中所有相似行的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57900276/

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