如何为查询mysql添加临时值
Posted
技术标签:
【中文标题】如何为查询mysql添加临时值【英文标题】:how to add a temp values for query mysql 【发布时间】:2022-01-09 01:52:50 【问题描述】:我的数据库中有以下查询,我想添加一个新列来处理学生的最终 Apperciation: 查询:
select student_name,q4.percentage
from (select q2.student_id,mark *100/total as Percentage
from (select class_id,sum(max_mark)as total
from course
group by(class_id)
)q1 ,
(select sum(mark) as mark,student_id
from grades
group by(student_id)
) q2
where q2.student_id in (select student_id
from student
where student.section_id in(select section_id
from section
where class_id=q1.class_id)
)
order by q2.student_id
) q4
inner join student on q4.student_id=student.student_id;
结果如下: enter image description here
STUDENT_NAME | PERCENTAGE |
---|---|
Massoud | 50.41667 |
Ali-Shbeeb | 84.16667 |
Mona | 75.2941 |
现在我只需要在结果中添加一个新列,就像一个奖项一样 所以表格是这样的:
student_name percetage award
mahmoud-kabbani 79.166667 B
Kareem-Alshaeer 54.583 c
【问题讨论】:
在栏目奖项中决定提出哪个角色的标准是什么? if percetage >80 is A if percetage >60 is B if percetage >40 is C else E Why not upload images of code/errors when asking a question?,所以我在文本中添加了图片示例。 【参考方案1】:您可以在下面的 case 语句中包含 1 列 -
select student_name,q4.percentage,
CASE WHEN q4.percentage > 80 THEN 'A'
WHEN q4.percentage > 60 THEN 'B'
WHEN q4.percentage > 40 THEN 'C'
ELSE 'E'
END award
from (select q2.student_id,mark *100/total as Percentage
from (select class_id,sum(max_mark)as total
from course
group by(class_id)
)q1 ,
(select sum(mark) as mark,student_id
from grades
group by(student_id)
) q2
where q2.student_id in (select student_id
from student
where student.section_id in(select section_id
from section
where class_id=q1.class_id)
)
order by q2.student_id
) q4
inner join student on q4.student_id=student.student_id;
【讨论】:
以上是关于如何为查询mysql添加临时值的主要内容,如果未能解决你的问题,请参考以下文章
如何为mysql中的数据记录添加一个唯一的编号,在查询的时候可以根据这个编号对这条记录进行操作。