首页  >  文章  >  数据库  >  SQL:select case 用法详解 带例子 图解说明 sqlserver2000

SQL:select case 用法详解 带例子 图解说明 sqlserver2000

WBOY
WBOY原创
2016-06-07 15:20:024321浏览

CASE 可能是SQL中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用 CASE 。 首先让我们看一下 CASE 的语法。在一般的 SELECT 中,其语法如下: SELECT myColumnSpec = CASE WHEN A

 

 

CASE 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用 CASE
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000首先让我们看一下 
CASE 的语法。在一般的 SELECT 中,其语法如下:
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SELECT <myColumnSpec> =
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
CASE
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
WHEN <A> THEN <somethingA>
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
WHEN <B> THEN <somethingB>
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
ELSE <somethingE>
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
END 
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子:
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
USE pubs
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
GO
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SELECT
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    Title,
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
'Price Range' =
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
CASE
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时 价格 NULL 那么 '未定价'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时 价格  10 然后 ' 特价'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时价格 之间  10  20 然后 '平均'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
其他 '给亲戚留下深刻印象的礼物'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
END
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
来自 标题
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
订购  价格
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
出发 

 

举例:

数据库酒吧

表格标题

标题     价格 

1          [NULL]

2          [NULL]

3          9

4          11

5           21

 

图解

SQL:select case 用法详解  带例子 图解说明    sqlserver2000

 

CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 CASE
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SELECT 'Number of Titles'Count(*)
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
FROM titles
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
GROUP BY
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
CASE
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
WHEN price IS NULL THEN 'Unpriced'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
WHEN price < 10 THEN 'Bargain'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
WHEN price BETWEEN 10 and 20 THEN 'Average'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
ELSE 'Gift to impress relatives'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
END
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
GO 

 

图解

SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000你甚至还可以组合这些选项,添加一个 
ORDER BY 子句,如下所示:
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
USE pubs
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
GO
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SELECT
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
CASE
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
WHEN price IS NULL THEN 'Unpriced'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
WHEN price < 10 THEN 'Bargain'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
WHEN price BETWEEN 10 and 20 THEN 'Average'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
ELSE 'Gift to impress relatives'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
END AS Range,
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    Title
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
FROM titles
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
GROUP BY
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
CASE
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时 价格 NULL 那么 '未定价'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时 价格  10 然后 ' 特价'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时价格 之间  10  20 然后 '平均'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
其他 '给亲戚留下深刻印象的礼物'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
END ,
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    标题
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
订单 作者
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
案例
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时 价格 NULL 然后 '未定价'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时 价格 10 然后 '讨价还价'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000        
何时 价格之间 10  20 然后 '平均'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
其他 '给亲戚留下深刻印象的礼物'
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    
END,
SQL:select case 用法详解  带例子 图解说明    sqlserver2000    标题
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
GO 

 

图解

SQL:select case 用法详解  带例子 图解说明    sqlserver2000GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000除了选择自定义字段之外,在很多情况下 
CASE 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集


SQL:select case 用法详解  带例子 图解说明    sqlserver2000
SQL:select case 用法详解  带例子 图解说明    sqlserver2000注意,为了在 


SQL:select case 用法详解  带例子 图解说明    sqlserver2000  

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn