首页 > 解决方案 > 如何在 JOIN 和 LEFT JOIN 表 MySQL 的联合中使用 AVG 和 COUNT php?

问题描述

我有一个产品列表,我在其中调用其他数据,例如类别、作者,现在我想假装显示评级。

我在里面做以下事情While

                $stmtR = $con->prepare("SELECT AVG(rating) AS avg_rating,
                                                COUNT(*) valuations
                                        FROM tbl_ratings
                                        WHERE id_course=?");
                $stmtR->bind_param("i",$id_course);
                $stmtR->execute();
                $stmtR->bind_result($avg_rating,$valuations);
                while ($stmtR->fetch()) {
                    $avg_print = number_format($avg_rating,1);
                        echo '<div class="review flex">
                            <div class="total">
                                <span>'.$avg_print.'</span>
                            </div>
                            <div class="rating">
                                '.display_star_print($avg_print).'
                            </div>
                            <div class="valuations"><span>('.$valuations.')</span></div>
                        </div>';
                }
                $stmtR->close();

但是利用我从表联合中调用所有数据这一事实,我正在尝试以下操作,以避免在内部进行查询While

    $stmt = $con->prepare("SELECT co.id_course,
                                    au.author,
                                    ca.program_lang,
                                    co.study_program,
                                    co.launch,
                                    co.start_date,
                                    co.release_date,
                                    co.title_course,
                                    co.subtitle_course,
                                    co.price_old,
                                    co.price,
                                    co.url,
                                    //(AVG(ra.rating) AS ra.avg_rating,
                                    COUNT(*) ra.valuations) this add additional to avoid doing a query within a loop, obviously without parentheses
                            FROM tbl_courses co
                            JOIN tbl_category ca ON co.category = ca.id_category
                            LEFT JOIN tbl_author au ON co.author = au.id_user
                            LEFT JOIN tbl_ratings ra ON co.id_course = ra.id_course
                            WHERE ca.program_lang = ? AND co.active = ?
                            ORDER BY co.id_course DESC
                            LIMIT ?");
    ...
    while ($stmt->fetch()) {
                $stmtR = $con->prepare("SELECT AVG(rating) AS avg_rating,
                                                COUNT(*) valuations
                                        FROM tbl_ratings
                                        WHERE id_course=?");
                $stmtR->bind_param("i",$id_course);
                $stmtR->execute();
                $stmtR->bind_result($avg_rating,$valuations);
                while ($stmtR->fetch()) {
                    $avg_print = number_format($avg_rating,1);
                        echo '<div class="review flex">
                            <div class="total">
                                <span>'.$avg_print.'</span>
                            </div>
                            <div class="rating">
                                '.display_star_print($avg_print).'
                            </div>
                            <div class="valuations"><span>('.$valuations.')</span></div>
                        </div>';
                }
                $stmtR->close();
    }

但这不会向我打印任何东西,这是使用它的正确方法,我想COUNT它可能会影响它,因为我不知道如何告诉它要计算什么,而是从表格中 tbl_ratings ra

错误:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=682257fd1529d3f58b4d7c6a43aa9f67

标签: phpmysql

解决方案


使用不带分组依据的 avg 或 count 表示您希望返回单个摘要行,其中包含所有课程的所有评分的计数和平均值。看起来您只想添加一个

GROUP BY co.id_course

在 ORDER BY 之前,因此您会为每门课程获得一行,其中包含该课程的 au.* 和 co.* 字段以及该课程的评分的 avg 和 count。


推荐阅读