mysql select case语句吗

2024-12-31 21:52:44
推荐回答(1个)
回答1:

MySql数据库不支持select case语句,但是它支持功能类似的case when函数。下面是函数的有关语法和使用示例:
语法1
CASE WHEN expr1 THEN v1 [WHEN expr2 THEN v2...][ELSE vn] END
例句:
select id,grade,case when grade>60 then 'good' when grade=60 then 'pass'
else 'fail' end as level from t1;
返回
id grade level
001 90 good
002 50 fail
003 60 pass
004 null fail

语法2
CASE expr WHEN e1 THEN v1 [WHEN e2 THEN v2...][ELSE vn] END
例句:
select id,grade,case grade when 90 then 'good' when 60 then 'pass'
when 50 then 'fail' else 'no grade' end as level from t1;
返回
id grade level
001 90 good
002 50 fail
003 60 pass
004 null no grade