Usage of case when in mysql

Here are three examples of how to use the case when statement in mysql for your reference and learning. If you are interested in the use of the case when statement in mysql, you may wish to take a look.

 

  1. select name,
  2. case
  3. when birthday<'1981' then 'old'
  4. when birthday>'1988' then 'yong'
  5. else 'ok' END YORN
  6. from lee;

 

  1. select NAME,
  2. case name
  3. when 'sam' then 'yong'
  4. when 'lee' then 'handsome'
  5. else 'good' end
  6. from lee;

Of course, the case when statement can also be compounded

  1. select name,birthday,
  2. case
  3. when birthday>'1983' then 'yong'
  4. when name='lee' then 'handsome'
  5. else 'just so so ' end
  6. from lee;

The above is an introduction to the usage example of the case when statement in mysql.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327015395&siteId=291194637