免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3458 | 回复: 0
打印 上一主题 下一主题

MySQL 的 CASE WHEN 语句 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-11 11:30 |只看该作者 |倒序浏览
  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. mysql> SELECT name AS Name,
  18.     -> CASE category
  19.     -> WHEN "Holiday" THEN "Seasonal"
  20.     -> WHEN "Profession" THEN "Bi_annual"
  21.     -> WHEN "Literary" THEN "Random" END AS "Pattern"
  22.     -> FROM sales;
  23. +------------+-----------+
  24. | Name       | Pattern   |
  25. +------------+-----------+
  26. | Java       | Seasonal  |
  27. | C          | Bi_annual |
  28. | JavaScript | Random    |
  29. | SQL        | Bi_annual |
  30. | Oracle     | Seasonal  |
  31. | MySQL      | Random    |
  32. | Cplus      | Random    |
  33. | Python     | Seasonal  |
  34. | PHP        | Bi_annual |
  35. +------------+-----------+
  36. 9 rows in set (0.00 sec)


  37. */
  38. Drop table sales;
  39.   
  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;
  66.            
复制代码


[代码] 在SELECT查询中使用CASE WHEN
  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. Drop table DVDs;

  26. CREATE TABLE DVDs (
  27.    ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  28.    Name VARCHAR(60) NOT NULL,
  29.    NumDisks TINYINT NOT NULL DEFAULT 1,
  30.    RatingID VARCHAR(4) NOT NULL,
  31.    StatID CHAR(3) NOT NULL
  32. )
  33. ENGINE=INNODB;

  34. INSERT INTO DVDs (Name, NumDisks, RatingID, StatID)
  35. VALUES ('Christmas', 1, 'NR', 's1'),
  36.        ('Doc',       1, 'G',  's2'),
  37.        ('Africa',    1, 'PG', 's1'),
  38.        ('Falcon',    1, 'NR', 's2'),
  39.        ('Amadeus',   1, 'PG', 's2'),
  40.        ('Show',      2, 'NR', 's2'),
  41.        ('View',      1, 'NR', 's1'),
  42.        ('Mash',      2, 'R',  's2');
  43.   

  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;
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP