我正在尝试找出以下各组的平均答案,但是我无法解决!
这是我的餐桌布置。
question_groups有很多问题
多对多的问题
问题有很多question_items
座位有很多提交文件
可容纳许多版本
提交的内容有很多答案
我想返回以下结构
{
name: "Comfort",
labels: [
"Seat A", "Seat B5", "Seat C", "Seat D"
]
data: [2.5, 2.9, 1.2, 4.5]
}
基本上,我将如何找到答案的平均值(按席位和问题组分组)。
我已经获得了下面的MySQL代码,但是它看起来不正确,然后我需要对其进行格式化以使其在上方显示。
SELECT question_groups.name as 'group', CONCAT(seats.name, ' - ',
versions.name) AS 'seat', avg(answers.value) AS 'value'
FROM question_groups
JOIN questions ON questions.question_group_id = questions.id
JOIN questions_seats ON questions_seats.question_id = questions.id
LEFT JOIN seats ON seats.id = questions_seats.seat_id
LEFT JOIN seats_versions ON seats.id = seats_versions.seat_id
LEFT JOIN versions ON versions.id = seats_versions.version_id
JOIN submissions ON submissions.seat_id = seats.id
JOIN answers ON submissions.id = answers.submission_id
GROUP BY question_groups.name, seats.name, versions.name
我想如果雄辩地使用API资源会更容易,但是我不知道如何处理多对多部分并获得组的平均值