mysql - sql - Is it possible to set a column value as per the value in another column? -


let's i'm making 'students' database , there's table called 'exams' in it. have following 2 columns- 'marks' , 'grade' in , want 'grade' column automatically receive value based on value entered in 'marks'. 'a' 'marks'>=90, 'b' 'marks'>=80 , on. there way that?

not easily. in mysql, can update:

update students     set grade = (case when marks >= 90 'a'                       when marks >= 80 'b'                        . . .                  end); 

but out-of-date next insert or update statement.

in mysql 5.7.6, can have column generated automatically:

alter table students     add grade char(1) (case when marks >= 90 'a'                                when marks >= 80 'b'                                 . . .                           end); 

this convenient, because column calculated @ query time automatically up-to-date.

in earlier versions, can similar view:

create view v_students     select s.*,            (case when marks >= 90 'a'                  when marks >= 80 'b'                   . . .             end) grade     students; 

this generated column, requires query view.

and finally, can store value, using triggers maintain when marks changes.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -