简体   繁体   English

如何在单个mysql select语句中使用Multiple where子句?

[英]How can I use Multiple where clauses in a single mysql select statement?

I have one table called marksheet where I stored students' marks. 我有一张叫作标记表的表格,用来存储学生的分数。 I want to take out 20% of mark scored out of each unit exam, 30% out of each weekly test and 50% out of final exam. 我想在每次单元考试中拿出20%的分数,在每周一次考试中拿出30%的分数,在期末考试中拿出50%的分数。 All the names of Unit Exams, Weekly Tests and Final exam are stored under one column name Name_of_exam . 单元考试,每周考试和期末考试的所有名称都存储在名称Name_of_exam一栏中。 I can use 我可以用

SELECT regd, 
       Sum(mark_score)/sum(Full_mark)*20 as scored 
FROM marksheet 
WHERE Name_of_exam='First Unit Exam' 
   OR Name_of_exam='Second Unit Exam' 
   OR Name_of_exam='Third Unit Exam' 
GROUP BY regd;

But the problem is that I have no idea how I can take out 30% of mark scored by students: Where (Name_of_exam='First Weekly Test or Name_of_exam='Second Weekly Test' or Name_of_exam='Third Weekly Test') and 50% of mark scored Where Name_of_exam='Final Exam' . 但是问题是我不知道如何拿出学生得分的30%: Where (Name_of_exam='First Weekly Test or Name_of_exam='Second Weekly Test' or Name_of_exam='Third Weekly Test')和50%分数得分, Where Name_of_exam='Final Exam' in the same select statement as I have to do multiple where clauses. 在同一个select语句中,因为我必须执行多个where子句。 Please help. 请帮忙。 I have also attached here how I did the calculation in excel sheet. 我在这里还附加了我如何在Excel工作表中进行计算。

该图显示了标记表

You can't use multiple WHERE parts, but always you can use logical operators to join conditions. 您不能使用多个WHERE部分,但始终可以使用逻辑运算符来加入条件。

This WHERE will find results with score under your treshholds: WHERE会在您的努力范围内找到分数得分的结果:

SELECT regd, names, subjects, 
  sum( score ) * (CASE WHEN (Name_of_exam='First Weekly Test or Name_of_exam='Second Weekly Test' or Name_of_exam='Third Weekly Test') THEN 0.2 
       WHEN ( Name_of_exam='First Unit Exam' or Name_of_exam='Second Unit Exam' OR Name_of_exam='Third Unit Exam')  THEN 0.3
       WHEN ( Name_of_exam='Final Exam' ) THEN 0.5 )
  AS scored
FROM marksheet
GROUP BY regd, names, subjects, test_type

Try this: 尝试这个:

SELECT names, subjects, 
SUM(
  CASE 
  WHEN name_of_exam = 'First Weekly Test' THEN (score/full_marks)*20 
  WHEN name_of_exam = 'First Unit Exam' THEN (score/full_marks)*30 
  WHEN name_of_exam = 'Final Unit Exam' THEN (score/full_marks)*50 
  END) AS scored
FROM marksheet
GROUP BY names, subjects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM