如何为查询 mysql 添加临时值
how to add a temp values for query mysql
我的数据库中有以下查询,我想添加一个新列来处理学生的最终感谢:
查询:
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
现在我只需要在结果中添加一个新列,比如奖励
所以 table 就像:
student_name percetage award
mahmoud-kabbani 79.166667 B
Kareem-Alshaeer 54.583 c
您可以包含 1 列 case 语句,如下所示 -
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;
我的数据库中有以下查询,我想添加一个新列来处理学生的最终感谢: 查询:
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 |
现在我只需要在结果中添加一个新列,比如奖励 所以 table 就像:
student_name percetage award
mahmoud-kabbani 79.166667 B
Kareem-Alshaeer 54.583 c
您可以包含 1 列 case 语句,如下所示 -
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;