SQL中使用CASE处理条件
在使用SQL的过程中,有时候可能会要对数据进行条件查询。比如字段A为True时查询出“男”,否则查询出“女”。这样就可以不在程序中在去判断了。在SQL中,可以利用Case语句来完成这样的查询。
一、Case介绍
CASE
计算条件列表并返回多个可能结果表达式之一。
CASE 具有两种格式:
简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。
CASE 搜索函数计算一组布尔表达式以确定结果。
两种格式都支持可选的 ELSE 参数。
语法
简单 CASE 函数:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
CASE 搜索函数:
CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
参数
input_expression
是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft® SQL Server™ 表达式。
WHEN when_expression
使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。
n
占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。
THEN result_expression
当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。
ELSE else_result_expression
当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。
WHEN Boolean_expression
使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。
二、SQL中的Case语句实例
CASE 函数用于计算多个条件并为每个条件返回单个值。CASE 函数通常的用途是使用可读性更强的值替换代码或缩写。下面的查询使用 CASE 函数重命名书籍的分类,以使之更易理解。
USE pubs
SELECT
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 AS Category,
CONVERT(varchar(30), title) AS "Shortened Title",
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY 1
下面是结果集:
category shortened title Price �
------------------- ------------------------------ -------
Business Cooking with Computers: Surrep 11.95 �
Business Straight Talk About Computers 19.99 �
Business The Busy Executive's Database 19.99 �
Business You Can Combat Computer Stress 2.99 �
Modern Cooking Silicon Valley Gastronomic Tre 19.99 �
Modern Cooking The Gourmet Microwave 2.99 �
Popular Computing But Is It User Friendly? 22.95 �
Popular Computing Secrets of Silicon Valley 20.00 �
Psychology Computer Phobic AND Non-Phobic 21.59 �
Psychology Emotional Security: A New Algo 7.99 �
Psychology Is Anger the Enemy? 10.95 �
Psychology Life Without Fear 7.00 �
Psychology Prolonged Data Deprivation: Fo 19.99 �
Traditional Cooking Fifty Years in Buckingham Pala 11.95 �
Traditional Cooking Onions, Leeks, and Garlic: Coo 20.95 �
Traditional Cooking Sushi, Anyone? 14.99
(16 row(s) affected)
CASE 函数的另一个用途给数据分类。下面的查询使用 CASE 函数对价格分类。
SELECT
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 AS "Price Category",
CONVERT(varchar(20), title) AS "Shortened Title"
FROM pubs.dbo.titles
ORDER BY price
下面是结果集:
Price Category Shortened Title �
--------------------- --------------------
Not yet priced The Psychology of Co
Not yet priced Net Etiquette �
Very Reasonable Title You Can Combat Compu
Very Reasonable Title The Gourmet Microwav
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 The Busy Executive's
Coffee Table Title Straight Talk About�
Coffee Table Title Silicon Valley Gastr
Coffee Table Title Prolonged Data Depri
Expensive book! Secrets of Silicon V
Expensive book! Onions, Leeks, and G
Expensive book! Computer Phobic AND�
Expensive book! But Is It User Frien
(18 row(s) affected)

本文来自: SQL条件处理--Case语句