使用CASE WHEN进行字符串替换处理

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
Java代码   收藏代码
  1. /* 
  2. mysql> select * from sales; 
  3. +-----+------------+--------+--------+--------+------+------------+ 
  4. | num | name       | winter | spring | summer | fall | category   | 
  5. +-----+------------+--------+--------+--------+------+------------+ 
  6. |   1 | Java       |   1067 |    200 |    150 |  267 | Holiday    | 
  7. |   2 | C          |    970 |    770 |    531 |  486 | Profession | 
  8. |   3 | JavaScript |     53 |     13 |     21 |  856 | Literary   | 
  9. |   4 | SQL        |    782 |    357 |    168 |  250 | Profession | 
  10. |   5 | Oracle     |    589 |    795 |    367 |  284 | Holiday    | 
  11. |   6 | MySQL      |    953 |    582 |    336 |  489 | Literary   | 
  12. |   7 | Cplus      |    752 |    657 |    259 |  478 | Literary   | 
  13. |   8 | Python     |     67 |     23 |     83 |  543 | Holiday    | 
  14. |   9 | PHP        |    673 |     48 |    625 |   52 | Profession | 
  15. +-----+------------+--------+--------+--------+------+------------+ 
  16. 9 rows in set (0.01 sec) 
  17.  
  18. mysql> SELECT name AS Name, 
  19.     -> CASE category 
  20.     -> WHEN "Holiday" THEN "Seasonal" 
  21.     -> WHEN "Profession" THEN "Bi_annual" 
  22.     -> WHEN "Literary" THEN "Random" END AS "Pattern" 
  23.     -> FROM sales; 
  24. +------------+-----------+ 
  25. | Name       | Pattern   | 
  26. +------------+-----------+ 
  27. | Java       | Seasonal  | 
  28. | C          | Bi_annual | 
  29. | JavaScript | Random    | 
  30. | SQL        | Bi_annual | 
  31. | Oracle     | Seasonal  | 
  32. | MySQL      | Random    | 
  33. | Cplus      | Random    | 
  34. | Python     | Seasonal  | 
  35. | PHP        | Bi_annual | 
  36. +------------+-----------+ 
  37. 9 rows in set (0.00 sec) 
  38. */  
  39. Drop table sales;  
  40.     
  41. CREATE TABLE sales(  
  42.     num MEDIUMINT NOT NULL AUTO_INCREMENT,  
  43.     name CHAR(20),  
  44.     winter INT,  
  45.     spring INT,  
  46.     summer INT,  
  47.     fall INT,  
  48.     category CHAR(13),  
  49.     primary key(num)  
  50. )type=MyISAM;  
  51.   
  52. insert into sales value(1'Java'1067 , 200150267,'Holiday');  
  53. insert into sales value(2'C',970,770,531,486,'Profession');  
  54. insert into sales value(3'JavaScript',53,13,21,856,'Literary');  
  55. insert into sales value(4'SQL',782,357,168,250,'Profession');  
  56. insert into sales value(5'Oracle',589,795,367,284,'Holiday');  
  57. insert into sales value(6'MySQL',953,582,336,489,'Literary');  
  58. insert into sales value(7'Cplus',752,657,259,478,'Literary');  
  59. insert into sales value(8'Python',67,23,83,543,'Holiday');  
  60. insert into sales value(9'PHP',673,48,625,52,'Profession');  
  61.   
  62. select * from sales;  
  63.   
  64. SELECT name AS Name,  
  65. CASE category  
  66. WHEN "Holiday" THEN "Seasonal"  
  67. WHEN "Profession" THEN "Bi_annual"  
  68. WHEN "Literary" THEN "Random" END AS "Pattern"  
  69. FROM sales;     

简单语句

Java代码   收藏代码
  1. SELECT CASE WHEN 10*2=30 THEN '30 correct'  
  2.    WHEN 10*2=40 THEN '40 correct'  
  3.    ELSE 'Should be 10*2=20'  
  4. END;  

多重表达式

Java代码   收藏代码
  1. SELECT CASE 10*2  
  2.    WHEN 20 THEN '20 correct'  
  3.    WHEN 30 THEN '30 correct'  
  4.    WHEN 40 THEN '40 correct'  
  5. END;  

在SELECT查询中使用CASE WHEN

Java代码   收藏代码
  1. /* 
  2. mysql> SELECT Name, RatingID AS Rating, 
  3.     ->    CASE RatingID 
  4.     ->       WHEN 'R' THEN 'Under 17 requires an adult.' 
  5.     ->       WHEN 'X' THEN 'No one 17 and under.' 
  6.     ->       WHEN 'NR' THEN 'Use discretion when renting.' 
  7.     ->       ELSE 'OK to rent to minors.' 
  8.     ->    END AS Policy 
  9.     -> FROM DVDs 
  10.     -> ORDER BY Name; 
  11. +-----------+--------+------------------------------+ 
  12. | Name      | Rating | Policy                       | 
  13. +-----------+--------+------------------------------+ 
  14. | Africa    | PG     | OK to rent to minors.        | 
  15. | Amadeus   | PG     | OK to rent to minors.        | 
  16. | Christmas | NR     | Use discretion when renting. | 
  17. | Doc       | G      | OK to rent to minors.        | 
  18. | Falcon    | NR     | Use discretion when renting. | 
  19. | Mash      | R      | Under 17 requires an adult.  | 
  20. | Show      | NR     | Use discretion when renting. | 
  21. | View      | NR     | Use discretion when renting. | 
  22. +-----------+--------+------------------------------+ 
  23. 8 rows in set (0.01 sec) 
  24. */  
  25.   
  26. Drop table DVDs;  
  27.   
  28. CREATE TABLE DVDs (  
  29.    ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,  
  30.    Name VARCHAR(60) NOT NULL,  
  31.    NumDisks TINYINT NOT NULL DEFAULT 1,  
  32.    RatingID VARCHAR(4) NOT NULL,  
  33.    StatID CHAR(3) NOT NULL  
  34. )  
  35. ENGINE=INNODB;  
  36.   
  37. INSERT INTO DVDs (Name, NumDisks, RatingID, StatID)  
  38. VALUES ('Christmas'1'NR''s1'),  
  39.        ('Doc',       1'G',  's2'),  
  40.        ('Africa',    1'PG''s1'),  
  41.        ('Falcon',    1'NR''s2'),  
  42.        ('Amadeus',   1'PG''s2'),  
  43.        ('Show',      2'NR''s2'),  
  44.        ('View',      1'NR''s1'),  
  45.        ('Mash',      2'R',  's2');  
  46.     
  47.   
  48. SELECT Name, RatingID AS Rating,  
  49.    CASE RatingID  
  50.       WHEN 'R' THEN 'Under 17 requires an adult.'  
  51.       WHEN 'X' THEN 'No one 17 and under.'  
  52.       WHEN 'NR' THEN 'Use discretion when renting.'  
  53.       ELSE 'OK to rent to minors.'  
  54.    END AS Policy  
  55. FROM DVDs  
  56. ORDER BY Name;  
  57.   
  58. #表的创建  
  59. CREATE TABLE `lee` (  
  60. `id` int(10) NOT NULL AUTO_INCREMENT,   
  61. `name` char(20) DEFAULT NULL,   
  62. `birthday` datetime DEFAULT NULL,   
  63. PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8  
  64.   
  65. #数据插入:  
  66. insert into lee(name,birthday) values ('sam','1990-01-01');  
  67. insert into lee(name,birthday) values ('lee','1980-01-01');  
  68. insert into lee(name,birthday) values ('john','1985-01-01');  
  69.   
  70.   
  71. #使用case when语句  
  72. select name,  
  73.  case  
  74.         when birthday<'1981' then 'old'  
  75.         when birthday>'1988' then 'yong'  
  76.         else 'ok' END YORN  
  77. from lee;  
  78.    
  79. select NAME,  
  80.  case name  
  81.      when 'sam' then 'yong'  
  82.         when 'lee' then 'handsome'  
  83.         else 'good' end  
  84. from lee;  
  85. #当然了case when语句还可以复合  
  86.   
  87. select name,birthday,  
  88.  case  
  89.      when birthday>'1983' then 'yong'  
  90.         when name='lee' then 'handsome'  
  91.         else 'just so so ' end  
  92. from lee;  
  93.   
  94. #在这里用sql语句进行日期比较的话,需要对年加引号。要不然可能结果可能和预期的结果会不同。我的mysql版本5.1  
  95.   
  96. #当然也可以用year函数来实现,以第一个sql为例  
  97.   
  98. select NAME,  
  99.  CASE  
  100.      when year(birthday)>1988 then 'yong'  
  101.         when year(birthday)<1980 then 'old'  
  102.         else 'ok' END  
  103. from lee;  
  104.   
  105. create table penalties  
  106. (  
  107.  paymentno INTEGER not NULL,  
  108.     payment_date DATE not null,  
  109.     amount DECIMAL(7,2) not null,  
  110.     primary key(paymentno)  
  111. )  
  112.   
  113. insert into penalties values(1,'2008-01-01',3.45);  
  114. insert into penalties values(2,'2009-01-01',50.45);  
  115. insert into penalties values(3,'2008-07-01',80.45);  
  116.   
  117. #对罚款登记分为三类,第一类low,包括大于0小于等于40的罚款,第二类moderate大于40  
  118. #到80之间的罚款,第三类high包含所有大于80的罚款。  
  119. #统计出属于low的罚款编号。  
  120.   
  121. #第一道题的解法与上面的相同  
  122. select paymentno,amount,  
  123.  case  
  124.      when amount>0 and amount<=40 then 'low'  
  125.         when amount>40 and amount<=80 then 'moderate'  
  126.         when amount>80 then 'high'  
  127.         else 'incorrect' end lvl  
  128. from `penalties`  
  129.   
  130. #统计出属于low的罚款编号。重点看这里的解决方法  
  131. #方法1.  
  132. select paymentno,amount  
  133. from `penalties`  
  134. where case  
  135.  when amount>0 and  amount<=40 then 'low'  
  136.     when amount>40 and amount<=80 then 'moderate'  
  137.     when amount>80 then 'high'  
  138.     else 'incorrect' end ='low';  
  139.   
  140. #方法2  
  141. select *  
  142. from (select paymentno,amount,  
  143.  case  
  144.      when amount>0 and amount<=40 then 'low'  
  145.         when amount>40 and amount<=80 then 'moderate'  
  146.         when amount>80 then 'high'  
  147.         else 'incorrect' end lvl  
  148. from `penalties`) as p  
  149. where p.lvl='low';  
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
7天前
|
C++
大小写转换——islower/isupper或者tolwer/toupper函数的用法
大小写转换——islower/isupper或者tolwer/toupper函数的用法
13 0
fmt.Printf() 如果 format 里结尾没有 \n,输出的字符串串结尾会带有一个 '%'
fmt.Printf() 如果 format 里结尾没有 \n,输出的字符串串结尾会带有一个 '%'
define语句换行\后不能有空格
define语句换行\后不能有空格
108 0
.replace(/-/g,"/")的用法
  /-/g正则表达式   g  代表  global    全部替换  var str1 ="2012-08-12 23:13"; str1 = str1.replace(/-/g,"/"); var date = new Date(str1 ); alert(date.
1369 0

热门文章

最新文章