博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL 如何利用一条语句实现类似于if-else条件语句的判断
阅读量:6996 次
发布时间:2019-06-27

本文共 845 字,大约阅读时间需要 2 分钟。

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

199,提价20%

2100-999之间,提价10%

31000-1999间,提价5%

4其他提价2%

 

[sql]
  1. update goods  
  2. set price = (  
  3. case   
  4.   when price between 0 and 99 then price * 1.2  
  5.   when price between 100 and 999 then price * 1.1  
  6.   when price between 1000 and 1999 then price * 1.05  
  7.   when price > 1999 then price * 1.02  
  8. end);  
  9. select * from goods;  

 

二、 编写一条select句,实现如下效果

   姓名

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

 1        张三  86   良好

 2       李四   98   优秀

 3       王五   72   及格

 4       那六   69   及格

 5       小幺   56   不及格

规则如下:

1>=90优秀

2>=80良好

3>=60及格

4<60不及格

 

 

[sql]
  1. select id as 学号, name as 姓名, score as 分数,   
  2. @<span style="font-family:宋体;">level</span>:=(  
  3.   case   
  4.     when score >= 90 then '优秀'  
  5.     when score >= 80 and score < 90 then '良好'  
  6.     when score >= 60 and score < 80 then '及格'  
  7.     when score < 60 then '不及格'  
  8.   end  
  9. )  
  10. as 等级  
  11. from scores;  

 

转载于:https://www.cnblogs.com/gscq073240/articles/9231881.html

你可能感兴趣的文章