本文介绍下,在mysql数据库中,有关case when语句的用法,介绍了case when语句的基础知识,并提供了相关实例,供大家学习参考,有需要的朋友不要错过 mysql数据库中CASE WHEN语句。 case when语句,用于计算条件列表并返回多个可能结果表达式之一。 CASE 具有两种格式: 简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。 CASE 搜索函数计算一组布尔表达式以确定结果。 语法 CASE 搜索函数: 参数 是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft? SQL Server? 表达式。 WHEN when_expression 使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。 占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。 THEN result_expression 当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。 ELSE else_result_expression 当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。 WHEN Boolean_expression 使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。 结果类型 从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。有关更多信息,请参见数据类型的优先顺序。 结果值 简单 CASE 函数: 返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。 如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。 返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。 如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。 下面分享一些mysql case when语句的例子。 A. 使用带有简单 CASE 函数的 SELECT 语句 例子,使用 CASE 函数更改图书分类显示。
注释,后来我试了一下不让用category=。 我使用的代码为:
结果集: Category Shortened Title Price avg Category Shortened Title Price avg Category Shortened Title Price avg Category Shortened Title Price avg Category Shortened Title Price avg (21 row(s) affected) B. 使用带有简单 CASE 函数和 CASE 搜索函数的 SELECT 语句 例子:根据图书的价格范围将价格(money 列)显示为文本注释。
结果集: Price Category Shortened Title (18 row(s) affected) C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数 例子,使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
结果集: Name au_id title_id Type 例如,可以在 WHERE 子句中使用 CASE。 首先,来看下 CASE 的语法。在一般的 SELECT 中,其语法如下: 以上代码,需要用具体的参数代替尖括号中的内容。 例子:
这是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。 比方说下面的 GROUP BY 子句中的 CASE:
注意,为了在 GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。 除了选择自定义字段之外,在很多情况下 CASE 都非常有用。 稍加深入,还可以得到以前认为不可能得到的分组排序结果集。 使用CASE WHEN进行字符串替换处理/* mysql> select * from sales; +-----+------------+--------+--------+--------+------+------------+ | num | name | winter | spring | summer | fall | category | +-----+------------+--------+--------+--------+------+------------+ | 1 | Java | 1067 | 200 | 150 | 267 | Holiday | | 2 | C | 970 | 770 | 531 | 486 | Profession | | 3 | JavaScript | 53 | 13 | 21 | 856 | Literary | | 4 | SQL | 782 | 357 | 168 | 250 | Profession | | 5 | Oracle | 589 | 795 | 367 | 284 | Holiday | | 6 | MySQL | 953 | 582 | 336 | 489 | Literary | | 7 | Cplus | 752 | 657 | 259 | 478 | Literary | | 8 | Python | 67 | 23 | 83 | 543 | Holiday | | 9 | PHP | 673 | 48 | 625 | 52 | Profession | +-----+------------+--------+--------+--------+------+------------+ 9 rows in set (0.01 sec) mysql> SELECT name AS Name, -> CASE category -> WHEN "Holiday" THEN "Seasonal" -> WHEN "Profession" THEN "Bi_annual" -> WHEN "Literary" THEN "Random" END AS "Pattern" -> FROM sales; +------------+-----------+ | Name | Pattern | +------------+-----------+ | Java | Seasonal | | C | Bi_annual | | JavaScript | Random | | SQL | Bi_annual | | Oracle | Seasonal | | MySQL | Random | | Cplus | Random | | Python | Seasonal | | PHP | Bi_annual | +------------+-----------+ 9 rows in set (0.00 sec) */ Drop table sales; CREATE TABLE sales( num MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(20), winter INT, spring INT, summer INT, fall INT, category CHAR(13), primary key(num) )type=MyISAM; insert into sales value(1, 'Java', 1067 , 200, 150, 267,'Holiday'); insert into sales value(2, 'C',970,770,531,486,'Profession'); insert into sales value(3, 'JavaScript',53,13,21,856,'Literary'); insert into sales value(4, 'SQL',782,357,168,250,'Profession'); insert into sales value(5, 'Oracle',589,795,367,284,'Holiday'); insert into sales value(6, 'MySQL',953,582,336,489,'Literary'); insert into sales value(7, 'Cplus',752,657,259,478,'Literary'); insert into sales value(8, 'Python',67,23,83,543,'Holiday'); insert into sales value(9, 'PHP',673,48,625,52,'Profession'); select * from sales; SELECT name AS Name, CASE category WHEN "Holiday" THEN "Seasonal" WHEN "Profession" THEN "Bi_annual" WHEN "Literary" THEN "Random" END AS "Pattern" FROM sales; 简单语句 SELECT CASE WHEN 10*2=30 THEN '30 correct' WHEN 10*2=40 THEN '40 correct' ELSE 'Should be 10*2=20' END; 多重表达式 SELECT CASE 10*2 WHEN 20 THEN '20 correct' WHEN 30 THEN '30 correct' WHEN 40 THEN '40 correct' END; 在SELECT查询中使用CASE WHEN /* mysql> SELECT Name, RatingID AS Rating, -> CASE RatingID -> WHEN 'R' THEN 'Under 17 requires an adult.' -> WHEN 'X' THEN 'No one 17 and under.' -> WHEN 'NR' THEN 'Use discretion when renting.' -> ELSE 'OK to rent to minors.' -> END AS Policy -> FROM DVDs -> ORDER BY Name; +-----------+--------+------------------------------+ | Name | Rating | Policy | +-----------+--------+------------------------------+ | Africa | PG | OK to rent to minors. | | Amadeus | PG | OK to rent to minors. | | Christmas | NR | Use discretion when renting. | | Doc | G | OK to rent to minors. | | Falcon | NR | Use discretion when renting. | | Mash | R | Under 17 requires an adult. | | Show | NR | Use discretion when renting. | | View | NR | Use discretion when renting. | +-----------+--------+------------------------------+ 8 rows in set (0.01 sec) */ Drop table DVDs; CREATE TABLE DVDs ( ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(60) NOT NULL, NumDisks TINYINT NOT NULL DEFAULT 1, RatingID VARCHAR(4) NOT NULL, StatID CHAR(3) NOT NULL ) ENGINE=INNODB; INSERT INTO DVDs (Name, NumDisks, RatingID, StatID) VALUES ('Christmas', 1, 'NR', 's1'), ('Doc', 1, 'G', 's2'), ('Africa', 1, 'PG', 's1'), ('Falcon', 1, 'NR', 's2'), ('Amadeus', 1, 'PG', 's2'), ('Show', 2, 'NR', 's2'), ('View', 1, 'NR', 's1'), ('Mash', 2, 'R', 's2'); SELECT Name, RatingID AS Rating, CASE RatingID WHEN 'R' THEN 'Under 17 requires an adult.' WHEN 'X' THEN 'No one 17 and under.' WHEN 'NR' THEN 'Use discretion when renting.' ELSE 'OK to rent to minors.' END AS Policy FROM DVDs ORDER BY Name; |