MySQL 的CASE WHEN 语句使用说明,需要的朋友可以参考下。

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

  1.  
  2. /*   
  3. mysql> select * from sales;   
  4. +-----+------------+--------+--------+--------+------+------------+   
  5. | num | name | winter | spring | summer | fall | category |   
  6. +-----+------------+--------+--------+--------+------+------------+   
  7. | 1 | Java | 1067 | 200 | 150 | 267 | Holiday |   
  8. | 2 | C | 970 | 770 | 531 | 486 | Profession |   
  9. | 3 | JavaScript | 53 | 13 | 21 | 856 | Literary |   
  10. | 4 | SQL | 782 | 357 | 168 | 250 | Profession |   
  11. | 5 | Oracle | 589 | 795 | 367 | 284 | Holiday |   
  12. | 6 | MySQL | 953 | 582 | 336 | 489 | Literary |   
  13. | 7 | Cplus | 752 | 657 | 259 | 478 | Literary |   
  14. | 8 | Python | 67 | 23 | 83 | 543 | Holiday |   
  15. | 9 | PHP | 673 | 48 | 625 | 52 | Profession |   
  16. +-----+------------+--------+--------+--------+------+------------+   
  17. rows in set (0.01 sec)   
  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. rows in set (0.00 sec)   
  38. */   
  39. Drop table sales;   
  40. CREATE TABLE sales(   
  41. num MEDIUMINT NOT NULL AUTO_INCREMENT,   
  42. name CHAR(20),   
  43. winter INT,   
  44. spring INT,   
  45. summer INT,   
  46. fall INT,   
  47. category CHAR(13),   
  48. primary key(num)   
  49. )type=MyISAM;   
  50. insert into sales value(1, 'Java', 1067 , 200, 150, 267,'Holiday');   
  51. insert into sales value(2, 'C',970,770,531,486,'Profession');   
  52. insert into sales value(3, 'JavaScript',53,13,21,856,'Literary');   
  53. insert into sales value(4, 'SQL',782,357,168,250,'Profession');   
  54. insert into sales value(5, 'Oracle',589,795,367,284,'Holiday');   
  55. insert into sales value(6, 'MySQL',953,582,336,489,'Literary');   
  56. insert into sales value(7, 'Cplus',752,657,259,478,'Literary');   
  57. insert into sales value(8, 'Python',67,23,83,543,'Holiday');   
  58. insert into sales value(9, 'PHP',673,48,625,52,'Profession');   
  59. select * from sales;   
  60. SELECT name AS Name,   
  61. CASE category   
  62. WHEN "Holiday" THEN "Seasonal"   
  63. WHEN "Profession" THEN "Bi_annual"   
  64. WHEN "Literary" THEN "Random" END AS "Pattern"   
  65. FROM sales;  

简单语句

  1.  
  2. SELECT CASE WHEN 10*2=30 THEN '30 correct'   
  3. WHEN 10*2=40 THEN '40 correct'   
  4. ELSE 'Should be 10*2=20'   
  5. END;  

多重表达式

  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

  1.  
  2. /*   
  3. mysql> SELECT Name, RatingID AS Rating,   
  4. -> CASE RatingID   
  5. -> WHEN 'R' THEN 'Under 17 requires an adult.'   
  6. -> WHEN 'X' THEN 'No one 17 and under.'   
  7. -> WHEN 'NR' THEN 'Use discretion when renting.'   
  8. -> ELSE 'OK to rent to minors.'   
  9. -> END AS Policy   
  10. -> FROM DVDs   
  11. -> ORDER BY Name;   
  12. +-----------+--------+------------------------------+   
  13. Name | Rating | Policy |   
  14. +-----------+--------+------------------------------+   
  15. | Africa | PG | OK to rent to minors. |   
  16. | Amadeus | PG | OK to rent to minors. |   
  17. | Christmas | NR | Use discretion when renting. |   
  18. | Doc | G | OK to rent to minors. |   
  19. | Falcon | NR | Use discretion when renting. |   
  20. | Mash | R | Under 17 requires an adult. |   
  21. | Show | NR | Use discretion when renting. |   
  22. View | NR | Use discretion when renting. |   
  23. +-----------+--------+------------------------------+   
  24. rows in set (0.01 sec)   
  25. */   
  26. Drop table DVDs;   
  27. CREATE TABLE DVDs (   
  28. ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,   
  29. Name VARCHAR(60) NOT NULL,   
  30. NumDisks TINYINT NOT NULL DEFAULT 1,   
  31. RatingID VARCHAR(4) NOT NULL,   
  32. StatID CHAR(3) NOT NULL   
  33. )   
  34. ENGINE=INNODB;   
  35. INSERT INTO DVDs (Name, NumDisks, RatingID, StatID)   
  36. VALUES ('Christmas', 1, 'NR''s1'),   
  37. ('Doc', 1, 'G''s2'),   
  38. ('Africa', 1, 'PG''s1'),   
  39. ('Falcon', 1, 'NR''s2'),   
  40. ('Amadeus', 1, 'PG''s2'),   
  41. ('Show', 2, 'NR''s2'),   
  42. ('View', 1, 'NR''s1'),   
  43. ('Mash', 2, 'R''s2');   
  44. SELECT Name, RatingID AS Rating,   
  45. CASE RatingID   
  46. WHEN 'R' THEN 'Under 17 requires an adult.'   
  47. WHEN 'X' THEN 'No one 17 and under.'   
  48. WHEN 'NR' THEN 'Use discretion when renting.'   
  49. ELSE 'OK to rent to minors.'   
  50. END AS Policy   
  51. FROM DVDs   
  52. ORDER BY Name;