简体   繁体   English

查询两个不同表 PHP 中两个字段的 SUM

[英]Query SUM for two fields in two different tables PHP

I am having a problem generating the result I need.我在生成我需要的结果时遇到问题。 I want to sum the other table gg_hp base on gg_id我想根据 gg_id 对另一个表 gg_hp 求和

+--------------+----------+-------+------------+
| sg_name      | sg_grade | gg_id | subject_id |
+--------------+----------+-------+------------+
| Quiz 1       |       20 |    14 |         68 |
| Midterm Exam |       50 |    15 |         68 |
| Quiz 2       |       50 |    14 |         68 |
+-------+--------------+----------+-------+----+

tbl_gradesubcateg
+-------+--------------+------------+------------+------------+------------+-------+
| gg_id | categname    | percentage | gradecount | teacher_id | subject_id | gg_hp |
+-------+--------------+------------+------------+------------+------------+-------+
|    14 | Quiz         |         10 |       NULL |          4 |         68 |     0 |
|    15 | Midterm Exam |         20 |       NULL |          4 |         68 |     0 |
+-------+--------------+------------+------------+------------+------------+-------+

This is my expected output
+-------+--------------+------------+------------+------------+------------+-------+
| gg_id | categname    | percentage | gradecount | teacher_id | subject_id | gg_hp |
+-------+--------------+------------+------------+------------+------------+-------+
|    14 | Quiz         |         10 |       NULL |          4 |         68 |   70 |
|    15 | Midterm Exam |         20 |       NULL |          4 |         68 |   50  |
+-------+--------------+------------+------------+------------+------------+-------+

This is the query I made but.. Im not getting accurate result.这是我提出的查询,但是.. 我没有得到准确的结果。

    $querycount = "SELECT * FROM tbl_gradesubcateg order by gg_id asc ";
                                            $query_run = mysqli_query($con,$querycount);

                                            $sums= 0;
                                            $ctr = 0;
                                            $id1 = "";
                                            while ($num = mysqli_fetch_assoc ($query_run)) {
                                                $sums += $num['sg_grade'];
                                                $id= $num['gg_id'];
                                                if($id == $id1)
                                                {
                                                    $queryhp = mysqli_query($con,"UPDATE tbl_gradecateg SET gg_hp = '".$sums."' where gg_id='".$id."'")  or die(mysqli_error($con));
                                                }
                                                else
                                                {
                                                    $queryhp = mysqli_query($con,"UPDATE tbl_gradecateg SET gg_hp = '".$sums."' where gg_id='".$id."'")  or die(mysqli_error($con));
                                                    $sums= 0;

                                                }
                                                $id1= $num['gg_id'];
                                            }
    ```

Any thoughts would be great.任何想法都会很棒。

A correlated subquery is a simple approach:相关子查询是一种简单的方法:

update tbl_gradesubcateg gs
    set sg_grade = (select sum(sg.g_grade)
                    from gg_hp g
                    where g.gg_id = gs.gg_id
                   );

I don't recommend doing this calculation, though.不过,我不建议进行此计算。 It is simple enough to aggregate when you query the table.查询表时进行聚合很简单。 Summarizing the results just means that they are out-of-date the next time rows are inserted, updated, or deleted in the tables.总结结果只是意味着下次在表中插入、更新或删除行时它们已经过时了。

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

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