There is a table with groups, objects, and the number of hours.

As a result of the algorithm, you should get the following
As you can see from the illustration, I managed to implement it. BUT! The implementation took place only on one condition.
$bd = mysql_query("SELECT * FROM list WHERE profile='Менеджмент' AND discipline='Информатика'"); while ($myrow = mysql_fetch_assoc($bd)) { $res = $myrow['type']; if ($res == 'Лекция') { $lec = $lec + $myrow['count_hours']; $countgroup++; $countstudents = $countstudents + $myrow['count_students']; } if ($res == 'Семинар') { $seminar = $seminar + $myrow['count_hours']; } if ($res == 'Лабораторная') { $lab = $lab + $myrow['count_hours']; $countpodgroup++; } $discipline = $myrow['discipline']; $profile = $myrow['profile']; $course_semestr = $myrow['course_semestr']; } $bde = mysql_query("UPDATE result SET id='$id', discipline='$discipline', profile='$profile', course_semestr='$course_semestr', count_students='$countstudents', count_group='$countgroup', count_podgroup='$countpodgroup', lectures='$lec',seminar='$seminar',lab='$lab'"); In the table, in addition to MANAGEMENT, there will be other meanings (Psychology, Computer Science, etc.) Therefore, you need to write an algorithm so that it shares these values, rather than adding them together. I understand that you need to wrap all this in a cycle?