MySQL CASE WHEN IF

MySQL CASE WHEN statements and IF statement example.

CREATE TABLE student (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(100) NOT NULL,
    marks int(11) NOT NULL,
    course_type varchar(2) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; 

SELECT name, marks, CASE
                       WHEN marks>75 THEN 'A'
                       WHEN marks>65 THEN 'B'
                       WHEN marks>50 THEN 'C'
                       ELSE  'F'
                    END
                    AS results 
FROM student ORDER BY name

SELECT name,course_type,
       CASE course_type
         WHEN 'WE' THEN 'Week End'
         WHEN 'WD' THEN 'Week Days'
       END AS course
FROM student ORDER BY name

SELECT name, marks, IF(marks >= 50,'PASS','FAIL') AS result
FROM student
ORDER BY name

No comments:

Post a Comment