gpt4 book ai didi

mysql - 对具有相同 id 的行求和 (mysql)

转载 作者:行者123 更新时间:2023-11-29 09:01:20 25 4
gpt4 key购买 nike

(最后一个)我想对具有相同 user_id 的行求和(对于列思考,我不需要其他列,只需 user_id 和思考)

enter image description here

我的查询

SELECT  students_group.user_id,scores.result ,exam.name,exam.ponderation,exam.id, (scores.result * exam.ponderation) / totpond.totalpond as ponderation
from (
select sum(exam.ponderation) as totalpond from scores
left join exam on exam.id = scores.exam_id
left join teacher_group on teacher_group.id = exam.teacher_group_id
left join groups on groups.id = teacher_group.group_school_id
left join students_group on students_group.user_id = scores.user_id
where teacher_group.id = 54
group by students_group.user_id

) as totpond,scores
left join exam on exam.id = scores.exam_id
left join teacher_group on teacher_group.id = exam.teacher_group_id
left join groups on groups.id = teacher_group.group_school_id
left join students_group on students_group.user_id = scores.user_id

where teacher_group.id = 54
group by scores.id
order by students_group.user_id

是否可以通过我的查询来做到这一点或者需要使用 php ?有人能帮助我吗 ?谢谢:)

最佳答案

没有必要,您只需要在查询中添加该字段的总和即可:

 SELECT 
tmp.user_id,
SUM(tmp.ponderation) as total_ponderation,
SUM(tmp.aggregate_ponderation) as ponderation
FROM
(
SELECT students_group.user_id,scores.result ,exam.name,exam.ponderation,exam.id, (scores.result * exam.ponderation) / totpond.totalpond as aggregate_ponderation
from (
select sum(exam.ponderation) as totalpond from scores
left join exam on exam.id = scores.exam_id
left join teacher_group on teacher_group.id = exam.teacher_group_id
left join groups on groups.id = teacher_group.group_school_id
left join students_group on students_group.user_id = scores.user_id
where teacher_group.id = 54
group by students_group.user_id

) as totpond,scores
left join exam on exam.id = scores.exam_id
left join teacher_group on teacher_group.id = exam.teacher_group_id
left join groups on groups.id = teacher_group.group_school_id
left join students_group on students_group.user_id = scores.user_id

where teacher_group.id = 54
group by scores.id
) tmp
GROUP BY
tmp.user_id

关于mysql - 对具有相同 id 的行求和 (mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8508948/

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