《MysqL数据库MysqL 的CASE WHEN 语句使用说明》要点:
本文介绍了MysqL数据库MysqL 的CASE WHEN 语句使用说明,希望对您有用。如果有疑问,可以联系我们。
MysqL数据库MysqL数据库中CASE WHEN语句.
MysqL数据库case when语句,用于计算条件列表并返回多个可能结果表达式之一.
MysqL数据库CASE 具有两种格式:
MysqL数据库简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果.
MysqL数据库CASE 搜索函数计算一组布尔表达式以确定结果.
两种格式都支持可选的 ELSE 参数.
MysqL数据库语法
简单 CASE 函数:
CASE input_Expression
WHEN when_Expression THEN result_Expression
[ ...n ]
[
ELSE else_result_Expression
END
MysqL数据库CASE 搜索函数:
CASE
WHEN Boolean_Expression THEN result_Expression
[ ...n ]
[
ELSE else_result_Expression
END
MysqL数据库参数
input_Expression
MysqL数据库是使用简单 CASE 格式时所计算的表达式.input_Expression 是任何有效的 Microsoft? sql Server? 表达式.
MysqL数据库WHEN when_Expression
MysqL数据库使用简单 CASE 格式时 input_Expression 所比较的简单表达式.When_Expression 是任意有效的 sql Server 表达式.input_Expression 和每个 when_Expression 的数据类型必须相同,或者是隐性转换.
MysqL数据库占位符,表明可以使用多个 WHEN when_Expression THEN result_Expression 子句或 WHEN Boolean_Expression THEN result_Expression 子句.
MysqL数据库THEN result_Expression
MysqL数据库当 input_Expression = when_Expression 取值为 TRUE,或者 Boolean_Expression 取值为 TRUE 时返回的表达式.
result Expression 是任意有效的 sql Server 表达式.
MysqL数据库ELSE else_result_Expression
MysqL数据库当比较运算取值不为 TRUE 时返回的表达式.如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NulL 值.Else_result_Expression 是任意有效的 sql Server 表达式.Else_result_Expression 和所有 result_Expression 的数据类型必须相同,或者必须是隐性转换.
MysqL数据库WHEN Boolean_Expression
MysqL数据库使用 CASE 搜索格式时所计算的布尔表达式.Boolean_Expression 是任意有效的布尔表达式.
MysqL数据库结果类型
MysqL数据库从 result_Expressions 和可选 else_result_Expression 的类型集合中返回最高的优先规则类型.有关更多信息,请参见数据类型的优先顺序.
MysqL数据库结果值
MysqL数据库简单 CASE 函数:
计算 input_Expression,然后按指定顺序对每个 WHEN 子句的 input_Expression = when_Expression 进行计算.
MysqL数据库返回第一个取值为 TRUE 的 (input_Expression = when_Expression) 的 result_Expression.
MysqL数据库如果没有取值为 TRUE 的 input_Expression = when_Expression,则当指定 ELSE 子句时 sql Server 将返回 else_result_Expression;若没有指定 ELSE 子句,则返回 NulL 值.
CASE 搜索函数:
按指定顺序为每个 WHEN 子句的 Boolean_Expression 求值.
MysqL数据库返回第一个取值为 TRUE 的 Boolean_Expression 的 result_Expression.
MysqL数据库如果没有取值为 TRUE 的 Boolean_Expression,则返回 NulL 值.
MysqL数据库下面分享一些MysqL case when语句的例子.
MysqL数据库A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较.
MysqL数据库例子,使用 CASE 函数更改图书分类显示.
代码如下:USE pubs
GO
SELECT category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'Trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(Title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM Titles
WHERE price IS NOT NulL
ORDER BY type,price
COmpuTE AVG(price) BY type
GO
MysqL数据库注释,后来我试了一下不让用category=.
MysqL数据库我使用的代码为:
SELECT
case gender
WHEN 1 THEN 'NAN'
WHEN 0 THEN 'NV'
end as gender
FROM
t_swIDy_day_nutrIEnt
MysqL数据库结果集:
MysqL数据库category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive's Data 19.99
Business Straight Talk About Compu 19.99
MysqL数据库 avg
==========================
13.73
MysqL数据库category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99
MysqL数据库 avg
==========================
11.49
MysqL数据库category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User FrIEndly? 22.95
MysqL数据库 avg
==========================
21.48
MysqL数据库category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59
MysqL数据库 avg
==========================
13.50
MysqL数据库category Shortened Title Price
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi,Anyone? 14.99
Traditional Cooking Onions,Leeks,and Garlic 20.95
MysqL数据库 avg
==========================
15.96
MysqL数据库(21 row(s) affected)
MysqL数据库B. 使用带有简单 CASE 函数和 CASE 搜索函数的
MysqL数据库 SELECT 语句
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换.
MysqL数据库例子:根据图书的价格范围将价格(money 列)显示为文本注释.
USE pubs
GO
SELECT 'Price category' =
CASE
WHEN price IS NulL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee table Title'
ELSE 'Expensive book!'
END,
CAST(Title AS varchar(20)) AS 'Shortened Title'
FROM Titles
ORDER BY price
GO
MysqL数据库结果集:
MysqL数据库Price category Shortened Title
--------------------- --------------------
Not yet priced Net Etiquette
Not yet priced The Psychology of Co
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title life Without Fear
Very Reasonable Title Emotional Security:
Coffee table Title Is Anger the Enemy?
Coffee table Title Cooking with Compute
Coffee table Title Fifty Years in Bucki
Coffee table Title Sushi,Anyone?
Coffee table Title Prolonged Data Depri
Coffee table Title Silicon Valley Gastr
Coffee table Title Straight Talk About
Coffee table Title The Busy Executive's
Expensive book! Secrets of Silicon V
Expensive book! Onions,and G
Expensive book! Computer Phobic And
Expensive book! But Is It User FrIEn
MysqL数据库(18 row(s) affected)
MysqL数据库C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
MysqL数据库例子,使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表.
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
RTRIM(a.au_lname) + ' '),1,25) AS name,a.au_ID,ta.Title_ID,
Type =
CASE
WHEN SUBSTRING(ta.Title_ID,2) = 'BU' THEN 'Business'
WHEN SUBSTRING(ta.Title_ID,2) = 'MC' THEN 'Modern Cooking'
WHEN SUBSTRING(ta.Title_ID,2) = 'PC' THEN 'Popular Computing'
WHEN SUBSTRING(ta.Title_ID,2) = 'PS' THEN 'Psychology'
WHEN SUBSTRING(ta.Title_ID,2) = 'TC' THEN 'Traditional Cooking'
END
FROM Titleauthor ta JOIN authors a ON ta.au_ID = a.au_ID
MysqL数据库结果集:
MysqL数据库name au_ID Title_ID Type
------------------------- ----------- -------- -------------------
Johnson White 172-32-1176 ps3333 Psychology
MarjorIE Green 213-46-8915 BU1032 Business
MarjorIE Green 213-46-8915 BU2075 Business
Cheryl Carson 238-95-7766 PC1035 Popular Computing
Michael O'Leary 267-41-2394 BU1111 Business
Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
Dean Straight 274-80-9391 BU7832 Business
Abraham Bennet 409-56-7008 BU1032 Business
Ann Dull 427-17-2319 PC8888 Popular Computing
Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
Charlene Locksley 486-29-1786 PC9999 Popular Computing
Charlene Locksley 486-29-1786 PS7777 Psychology
Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
Innes del Castillo 712-45-1867 MC2222 Modern Cooking
Michel DeFrance 722-51-5454 MC3021 Modern Cooking
Stearns MacFeather 724-80-9391 BU1111 Business
Stearns MacFeather 724-80-9391 PS1372 Psychology
livia Karsen 756-30-7391 PS1372 Psychology
Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
Sheryl Hunter 846-92-7186 PC8888 Popular Computing
Anne Ringer 899-46-2035 MC3021 Modern Cooking
Anne Ringer 899-46-2035 PS2091 Psychology
Albert Ringer 998-72-3567 PS2091 Psychology
Albert Ringer 998-72-3567 PS2106 Psychology
(25 row(s) affected)
CASE 可能是 sql 中被误用最多的关键字之一.
虽然,可能以前用过这个关键字来创建字段,但是它还具有更多用法.
MysqL数据库例如,可以在 WHERE 子句中使用 CASE.
MysqL数据库首先,来看下 CASE 的语法.在一般的 SELECT 中,其语法如下:
SELECT <myColumnspec> =
CASE
WHEN <A> THEN <somethingA>
WHEN <B> THEN <somethingB>
ELSE <somethingE>
END
MysqL数据库以上代码,需要用具体的参数代替尖括号中的内容.
MysqL数据库例子:
代码如下:USE pubs
GO
SELECT
Title,
'Price Range' =
CASE
WHEN price IS NulL THEN 'Unpriced'
WHEN price < 10 THEN 'bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END
FROM Titles
ORDER BY price
GO
MysqL数据库这是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情.
MysqL数据库比方说下面的 GROUP BY 子句中的 CASE:
SELECT 'Number of Titles',Count(*)
FROM Titles
GROUP BY
CASE
WHEN price IS NulL THEN 'Unpriced'
WHEN price < 10 THEN 'bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END
GO
甚至还可以组合这些选项,添加一个 ORDER BY 子句,例如:
代码如下:
USE pubs
GO
SELECT
CASE
WHEN price IS NulL THEN 'Unpriced'
WHEN price < 10 THEN 'bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END AS Range,
Title
FROM Titles
GROUP BY
CASE
WHEN price IS NulL THEN 'Unpriced'
WHEN price < 10 THEN 'bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END,
Title
ORDER BY
CASE
WHEN price IS NulL THEN 'Unpriced'
WHEN price < 10 THEN 'bargain'
WHEN price BETWEEN 10 and 20 THEN 'Average'
ELSE 'Gift to impress relatives'
END,
Title
GO
MysqL数据库注意,为了在 GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块.
MysqL数据库除了选择自定义字段之外,在很多情况下 CASE 都非常有用.
MysqL数据库稍加深入,还可以得到以前认为不可能得到的分组排序结果集.
使用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','NR','s1'),
('Doc','G','s2'),
('Africa','PG',
('Falcon',
('Amadeus',
('Show',2,
('VIEw',
('Mash','R','s2');
SELECT name,
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;
总结
以上是内存溢出为你收集整理的MYSQL数据库MySQL 的CASE WHEN 语句使用说明全部内容,希望文章能够帮你解决MYSQL数据库MySQL 的CASE WHEN 语句使用说明所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)