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;

MySQL中CASE WHEN THEN用法_第1张图片

你可能感兴趣的:(MySQL)