php - mysqli 每次只工作 else 条件但不工作 if 条件

标签 php mysql codeigniter mysqli

这是我的查询

CREATE VIEW marksheet as
SELECT name as name, student_id as student_id, 
roll as roll, class as class,exam_year as exam_year, 
subject_name as subject, exam_type as exam_type,
sum(full_mark) as full_mark, sum(getmark) as getmark,
department as department,
IF(SUM(IF(gpa='f' OR gpa='F',-9999,gpa))>=0, 
CAST(IF(subject_type=1,SUM(gpa)-2/count(subject_name),SUM(gpa)/count(subject_name)) 
AS CHAR), 'F') as total_gpa
FROM mark
GROUP by roll, class, exam_type

不工作

IF(subject_type=1,SUM(gpa)-2/count(subject_name),SUM(gpa)/count(subject_name))

每次只工作else条件SUM(gpa)/count(subject_name

不行subject_type=1,SUM(gpa)-2/count(subject_name)

我的 table

enter image description here

成绩:GPA = 5+8+4+6

         = 23

但是 subject_type = 1 所以,减去 -2 (不起作用)

         = 21 (Not work)

最终 Gpa = 21/计数(科目名称)

最佳答案

如果只能有一个额外主题(即具有 subject_type=1 的主题),而所有其他主题都具有 subject_type=0,则可以使用 MAX( subject_type) 来确定学生是否选修了额外的科目。此查询应该执行您想要的操作(请注意,您还需要 SUM(gpa)-2 周围的 ()):

CREATE VIEW marksheet as
SELECT name as name, student_id as student_id, 
roll as roll, class as class,exam_year as exam_year, 
subject_name as subject, exam_type as exam_type,
sum(full_mark) as full_mark, sum(getmark) as getmark,
department as department,
IF(SUM(IF(gpa='f' OR gpa='F',-9999,gpa))>=0, 
CAST(IF(MAX(subject_type)=1,(SUM(gpa)-2)/count(subject_name),SUM(gpa)/count(subject_name)) 
AS CHAR), 'F') as total_gpa
FROM mark
GROUP by roll, class, exam_type

我在 SQLFiddle 创建了一个简化的演示.

关于php - mysqli 每次只工作 else 条件但不工作 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53277421/

相关文章:

php - 使用 codeigniter 在 Twitter Bootstrap 模式中填充表单

php - 如何检查是否使用 Codeigniter 设置了 $data 变量?

php - 如何回显数据库中查询的结果?

php - 如何用 and 条件选择几个值?

php - 具有POST方法和httpurlconnection的Android到Php服务器

mysql - 获取特定日期的两个不同列的唯一值

c++ - 在 MySql 中使用字符串操作函数会出现什么问题?

php - VoiceXML - 如何同时提交录音文件和文本输入

php - 使用 MySQL 数据库中的详细信息将动态创建的一组图像中的数据动态传递到 Modal

php - CodeIgniter SQL 查询不返回任何内容