简体   繁体   English

PHP / SQL JOIN 2表并获得平均分

[英]PHP / SQL JOIN 2 tables and get an average score

I'm trying to present data collected from people who have attended training courses in 2017 and have given feedback on the course. 我正在尝试展示从2017年参加培训课程并提供反馈的人那里收集的数据。

I want to present a list of all the courses that ran in 2017 and next to each course give the average rating. 我想提供2017年所有课程的清单,每门课程旁边给出平均评分。

The two tables that I'm working with are 我正在使用的两个表是

courses
course_id   course_name
---------------------------
1         | Public speaking
2         | Social media skills


feedback
course_id   overall_rating
--------------------------
1         |  3
1         |  5
1         |  4
1         |  4
2         |  3
2         |  3
2         |  4

I can get the list of courses with 我可以得到课程清单

$yearscourses = "SELECT courseid,coursetitle FROM courses WHERE coursedate1 BETWEEN '2017-01-01' AND '2017-12-31'";
$yearsresult = mysqli_query($connect, $yearscourses);

And the average rating with 和平均评分

$avgrating = "SELECT AVG(overall_rating) FROM feedback WHERE courseid='courseid'";

But I'm struggling to present this in a table using HTML / PHP. 但是我很努力地使用HTML / PHP在表中显示此内容。 I tried the following but it just repeats the same average in every row. 我尝试了以下方法,但每行都重复相同的平均值。

<table cellspacing="0" table border="1" cellpadding="padding:5px;">
<tr style="font-size:20px; font-weight:bold; text-align:center;">
<td colspan="4">
2017
</td>
</tr><tr style="font-weight:bold;">
<td style="background-color:#AED6F1;">Course ID</td><td style="background-color:#AED6F1;">Course title</td><td style="background-color:#AED6F1;">Average rating</td>
</tr>

<?php

// Get a list of all courses for the year
$yearscourses = "SELECT courseid,coursetitle FROM courses WHERE coursedate1 BETWEEN '2017-01-01' AND '2017-12-31'";
$yearsresult = mysqli_query($connect, $yearscourses);


if (mysqli_num_rows($yearsresult) != 0) // Search has found results
{
$str = "\n";

}

while ($row = mysqli_fetch_array($yearsresult, MYSQLI_ASSOC)) {

// Get average rating for each course
$avgrating = "SELECT AVG(overall_rating) FROM feedback WHERE courseid='courseid'";
$avgresult = mysqli_query($connect, $avgrating);
foreach($avgresult as $row2) {

echo "<tr><td>" . $row['courseid']. "</td><td>" .$row['coursetitle']. "</td><td>".$row2['AVG(overall_rating)']."</td></tr>\n";
    $str .= " ";
}
}
$str .= "</table>\n</div>";
echo $str;


?>

use alias for average 平均使用别名

 $avgrating = "SELECT AVG(overall_rating) as average_rating FROM feedback WHERE courseid='courseid'"; // use alias for avereage

and in loop 并循环

echo "<tr><td>" . $row['courseid']. "</td><td>" .$row['coursetitle']. "</td><td>".$row2['average_rating']."</td></tr>\n";

As pointed out by the other post, you can use an alias to easily access the value in your loop. 正如另一篇文章所指出的,您可以使用别名轻松访问循环中的值。 That being said, you should avoid performing queries inside your loop. 话虽如此,您应该避免在循环内执行查询。 You can retrieve the same info by taking advantage of the GROUP BY clause. 您可以利用GROUP BY子句检索相同的信息。

Here is how it would go when joining the feedback table: 这是加入feedback表时的处理方式:

SELECT c.courseid, c.coursetitle, AVG(f.overall_rating) AS average_rating 
FROM courses c 
LEFT JOIN feedback f ON f.courseid = c.courseid
WHERE c.coursedate1 BETWEEN '2017-01-01' AND '2017-12-31'
GROUP BY f.courseid

Note that with this query, courses that did not receive any feedback will have a NULL value for the average_rating . 请注意,对于此查询,未收到任何反馈的课程的average_rating值为NULL

you just need to do this 你只需要这样做

$avgrating = "SELECT AVG(overall_rating) FROM feedback WHERE courseid='".$row['courseid']."'";

instead of that 代替那个

$avgrating = "SELECT AVG(overall_rating) FROM feedback WHERE courseid='courseid'";

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

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