如果调味品和调味品不起作用

时间:2018-11-10 04:42:58

标签: php mysql codeigniter mysqli

我有mark表。我在这里存储所有主题标记。我存储like gpa=3.5gpa=F 现在我想制作marksheet,所以我需要group by。我想如果一个学生上任何课程gpa =F,然后成绩单只显示Fail/f

在此处查询

CREATE VIEW result as SELECT name as name,roll as roll,class as 
class,subject_name as subject,exam_type as exam_type,sum(full_mark) as 
full_mark,sum(getmark) as getmark, if(gpa =='f', 'F',sum(gpa)) as 
total_gpa FROM mark GROUP by roll,class,exam_type

表格

enter image description here

1 个答案:

答案 0 :(得分:1)

您遇到的问题是,在IF之后应用SUM,而SUM仅将字符串(例如'f')的gpa值视为0。值意味着SUM始终是数字(并且永远不等于'f')。您需要做的是SUM时检查gpa值,然后在这种情况下使用特殊值(例如-9999)进行计算,例如<0,那么您可以测试总和<0,并以此为条件给出“ F”结果,例如

IF(SUM(IF(gpa='f' OR gpa='F',-9999,gpa)) >= 0,
      CAST(SUM(gpa) AS CHAR), 'F') AS total_gpa

请注意,因为IF的一个输出是文本,所以我们也必须将SUM转换为文本才能获得正确的结果。

我在SQLFiddle上创建了一个小型演示:

create table grades (id int, gpa varchar(20));
insert into grades values (1, 4), (1, 5), (2, 3), (2, 'f');
select IF(SUM(IF(gpa='f' OR gpa='F',-9999,gpa)) >= 0,
          CAST(SUM(gpa) AS CHAR), 'F') AS total_gpa
from grades 
group by id

输出:

total_gpa
9
F