Shibin
Shibin

Reputation: 248

Return boolean on mysql select statment after comparing values of particular column

I have a module table which has weightage and course id columns. Multiple modules with different weights can be added to a course. I need to compare the weights of all modules added to a course and return true if all weights are equal, false otherwise.


id   name   weight   course_id
 1    A       30       112
 2    B       40       112
 3    C       20        87
 4    D       23        87
 5    E       10        97
 6    F       10        97

I need the select query to return true if the weights for the modules(E and F), added to the course with id 97 are equal, false otherwise

Thanks in advance

Upvotes: 1

Views: 2302

Answers (2)

Madhivanan
Madhivanan

Reputation: 13700

Another method

select 
  course_id, 1 as matched
from my_table
group by course_id
Having min(weight)=max(weight)

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 23982

I need to compare the weights of all modules added to a course and return true if all weights are equal, false otherwise

Try this:

select 
  course_id, count( distinct weightage ) = 1 as matched
from my_table
group by course_id  

Upvotes: 2

Related Questions