Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the same ID get the same grade for different subjects?

Tags:

php

mysql

ID    | Subject | Grade

12113   BM         A     
12113   BI         A    
12113   MM         A    
12113   SJ         A    
12113   GE         B    
12113   SV         A    
12113   PJ         A    
12112   BM         A     
12112   BI         A    
12112   MM         A    
12112   SJ         A    
12112   GE         A    
12112   SV         A    
12112   PJ         A 

Hie all, above are the sample table of showing the studentID with multiple subject takens. I couldnt figure out how to use mysql query to check if the same studentID has score A for all subjects.

was trying to use HAVING MAX(Grade) = 'A' but doesnt help. any advice?

like image 866
meowwithteef Avatar asked Apr 12 '12 07:04

meowwithteef


2 Answers

select ID, 
case when count(ID) = sum(case when Grade = 'A' then 1 else 0 end) then 'YES' else 'NO' end as IsAllAGrade
from yourTable
group by ID
like image 140
Vikram Avatar answered Oct 23 '22 09:10

Vikram


This will return all students that have A in all subjects:

select distinct id from t t1
where not exists (
  select * from t t2
  where t1.id = t2.id and grade != 'A')

If you want to check for a particular student just add a where clause. By the way... can a student have more than one result for a given subject? (Twice the same subject but with different grade)

You can check the fiddle here.

like image 23
Mosty Mostacho Avatar answered Oct 23 '22 10:10

Mosty Mostacho