1. 程式人生 > >MySQL中CASE WHEN THEN用法

MySQL中CASE WHEN THEN用法

MySQL中CASE WHEN THEN用於分類統計

1、建立一個表
create table user(
    id int auto_increment primary key,
    age tinyint unsigned not null
);

 

2、新增一些資料

insert into user(age) values(12),(15),(20),(30),(35),(19),(24),(8),(61),(14);


3、CASE WHEN THEN
select id,age,(case when age<18 then '少年'

when age>=18 then '成年' end) as type  from user;