mysql 多个if_mysql if else 多条件

MySQL 如何利用一条语句实现类似于if-else条件语句的判断

一、 编写一条update语句实现商品涨价,具体规则如下

1、99元以内,提价20%

2、100-999元之间,提价10%

3、1000-1999之间,提价5%

4、其他提价2%update goods

set price = (

case

when price between 0 and 99 then price * 1.2

when price between 100 and 999 then price * 1.1

when price between 1000 and 1999 then price * 1.05

when price > 1999 then price * 1.02

end);

select * from goods;

二、 编写一条select语句,实现如下效果学号 姓名 分数 等级

-------------------------------------------------

1 张三 86 良好

2 李四 98 优秀

3 王五 72 及格

4 那六 69 及格

5 小幺 56 不及格

规则如下:

1、>=90:优秀

2、>=80:良好

3、>=60:及格

4、<60:不及格select id as 学号, name as 姓名, score as 分数,

(

case

when score >= 90 then 优秀

when score >= 80 and score < 90 then 良好

when score >= 60 and score < 80 then 及格

when score < 60 then 不及格

end

)

as 等级

from scores;

MySQL 如何利用一条语句实现类似于if-else条件语句的判断 一、 编写一条update语句实现商品涨价,具体规则如下 1、99元以内,提价20% 2、100-999元之间,提价10% 3、1000-1999之间,提价5% 4、其他提价2%update goods set price = ( case when price between 0 and 99 then price * 1.2 when price between 100 and 999 then price * 1.1 when price between 1000 and 1999 then price * 1.05 when price > 1999 then price * 1.02 end); select * from goods; 二、 编写一条select语句,实现如下效果学号 姓名 分数 等级 ------------------------------------------------- 1 张三 86 良好 2 李四 98 优秀 3 王五 72 及格 4 那六 69 及格 5 小幺 56 不及格 规则如下: 1、>=90:优秀 2、>=80:良好 3、>=60:及格 4、<60:不及格select id as 学号, name as 姓名, score as 分数, ( case when score >= 90 then 优秀 when score >= 80 and score < 90 then 良好 when score >= 60 and score < 80 then 及格 when score < 60 then 不及格 end ) as 等级 from scores;
经验分享 程序员 微信小程序 职场和发展