简体   繁体   English

无法获取SQL中所有相似行的总和

[英]Cannot get sum of all similar rows in sql

I have a booking table of student which list all the teacher he has booked. 我有一个学生预订表,其中列出了他所预订的所有老师。 Structure is as follows: 结构如下:

Teacher Name    Date     start_time   end_time     hour       rate

Now I want to list the booking as follows I want to calculate total rate which the student paid to every teacher and total of number of hours booked for each teacher. 现在,我想按以下方式列出预订,我要计算学生向每位老师支付的总费用以及为每位老师预订的总工作时间。 I want to do it with one query I'm using php with mysql. 我想用一个查询来做到这一点,我在mysql中使用php。

 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

I don't think you can query that detailed part where you show the booked classes unless you are going to add some functions on the html part. 我认为您无法查询显示预订的类的详细部分,除非您要在html部分上添加一些功能。

Edit --------------------- 编辑---------------------

Try this 尝试这个

 <?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> 

Result: 结果:

在此处输入图片说明

Although, it will be a whole lot easier thing if you would've declared the start_time and end_time as date time. 虽然,如果将start_time和end_time声明为日期时间,则要容易得多。

In the below line of code I had to change the string in normal time format and then perform the basic summation of time: 在下面的代码行中,我必须更改正常时间格式的字符串,然后执行时间的基本求和:

SELECT TeacherName, SUM(Rate) 
SEC_TO_TIME(SUM(TIME_TO_SEC
(TIMEDIFF(STR_TO_DATE(CONCAT(`date`, ' ',
REPLACE(`end_time`, '.', '')), '%Y-%m-%d %h:%i %p'), STR_TO_DATE(CONCAT(`date`, ' ',
REPLACE(`start_time`, '.', '')), '%Y-%m-%d %h:%i %p'))
)
)
) AS tot_time
FROM teachers
GROUP BY TeacherName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM