gpt4 book ai didi

php - 在左连接上显示 AVG

转载 作者:行者123 更新时间:2023-11-29 09:50:07 24 4
gpt4 key购买 nike

我想在左连接上显示 StudentQuizReading、StudentQuizLanguage 和 StudentQuizMath 表的 Score 字段的平均值。

我怎样才能做到这一点?

这是迄今为止我的代码:

$query = "
SELECT s.StudentID
, s.LastName
, s.FirstName
, s.MiddleName
, s.Level
, s.Adviser
, r.Score
, l.Score
, m.Score
FROM Students s
LEFT
JOIN StudentQuizReading r
ON r.StudentID = s.StudentID
LEFT
JOIN StudentQuizLanguage l
ON l.StudentID = s.StudentID
LEFT
JOIN StudentQuizMath m
ON m.StudentID = s.StudentID
WHERE s.StudentID = '".$_POST["StudentID"]."'
";

表的详细信息:StudentQuizReading、StudentQuizLanguage、StudentQuizMath

+-----------+-------------+
| Field | Type |
+-----------+-------------+
| StudentID | int(5) |
| QuizNum | varchar(10) |
| Score | int(2) |
+-----------+-------------+

最佳答案

您可以检查LEFT连接表列中StudentIDnull并可以找到平均值。

检查下面的查询。

$query = "SELECT
Students.StudentID,
Students.LastName,
Students.FirstName,
Students.MiddleName,
Students.Level,
Students.Adviser,
FirstQuarterGrades.Reading,
FirstQuarterGrades.Language,
FirstQuarterGrades.Mathematics,
(SUM(IF(StudentQuizReading.StudentID IS NULL, 0, StudentQuizReading.Score)) / SUM(IF(StudentQuizReading.StudentID IS NULL, 0, 1))) AS StudentQuizReadingAvg,
(SUM(IF(StudentQuizLanguage.StudentID IS NULL, 0, StudentQuizLanguage.Score)) / SUM(IF(StudentQuizLanguage.StudentID IS NULL, 0, 1))) AS StudentQuizLanguageAvg,
(SUM(IF(StudentQuizMath.StudentID IS NULL, 0, StudentQuizMath.Score)) / SUM(IF(StudentQuizMath.StudentID IS NULL, 0, 1))) AS StudentQuizMathAvg
FROM Students
LEFT JOIN StudentQuizReading ON Students.StudentID = StudentQuizReading.StudentID
LEFT JOIN StudentQuizLanguage ON Students.StudentID = StudentQuizLanguage.StudentID
LEFT JOIN StudentQuizMath ON Students.StudentID = StudentQuizMath.StudentID
WHERE Students.StudentID = '".$_POST["StudentID"]."'";

关于php - 在左连接上显示 AVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54958989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com