码迷,mamicode.com
首页 > 数据库 > 详细

sql case when 用法

时间:2014-07-11 21:21:53      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   strong   

sql语言中有没有类似C语言中的switch case的语句??

  没有,用case   when   来代替就行了.  
      
  例如,下面的语句显示中文年月  
   
  select   getdate()   as   日期,case   month(getdate())  
  when   11   then   ‘十一‘  
  when   12   then   ‘十二‘  
  else   substring(‘一二三四五六七八九十‘,   month(getdate()),1)  
  end+‘月‘   as   月份

=================================================

CASE 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用 CASE
bubuko.com,布布扣
bubuko.com,布布扣首先让我们看一下
CASE 的语法。在一般的 SELECT 中,其语法如下:
bubuko.com,布布扣
bubuko.com,布布扣
SELECT <myColumnSpec> =
bubuko.com,布布扣
CASE
bubuko.com,布布扣
WHEN <A> THEN <somethingA>
bubuko.com,布布扣
WHEN <B> THEN <somethingB>
bubuko.com,布布扣
ELSE <somethingE>
bubuko.com,布布扣
END
bubuko.com,布布扣
bubuko.com,布布扣在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子:
bubuko.com,布布扣
bubuko.com,布布扣
USE pubs
bubuko.com,布布扣
GO
bubuko.com,布布扣
SELECT
bubuko.com,布布扣     Title,
bubuko.com,布布扣   
Price Range =
bubuko.com,布布扣   
CASE
bubuko.com,布布扣       
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣       
WHEN price < 10 THEN Bargain
bubuko.com,布布扣       
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣       
ELSE Gift to impress relatives
bubuko.com,布布扣   
END
bubuko.com,布布扣
FROM titles
bubuko.com,布布扣
ORDER BY price
bubuko.com,布布扣
GO
bubuko.com,布布扣
bubuko.com,布布扣这是
CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 CASE
bubuko.com,布布扣
bubuko.com,布布扣
SELECT Number of Titles, Count(*)
bubuko.com,布布扣
FROM titles
bubuko.com,布布扣
GROUP BY
bubuko.com,布布扣   
CASE
bubuko.com,布布扣       
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣       
WHEN price < 10 THEN Bargain
bubuko.com,布布扣       
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣       
ELSE Gift to impress relatives
bubuko.com,布布扣   
END
bubuko.com,布布扣
GO
bubuko.com,布布扣
bubuko.com,布布扣你甚至还可以组合这些选项,添加一个
ORDER BY 子句,如下所示:
bubuko.com,布布扣
bubuko.com,布布扣
USE pubs
bubuko.com,布布扣
GO
bubuko.com,布布扣
SELECT
bubuko.com,布布扣   
CASE
bubuko.com,布布扣       
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣       
WHEN price < 10 THEN Bargain
bubuko.com,布布扣       
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣       
ELSE Gift to impress relatives
bubuko.com,布布扣   
END AS Range,
bubuko.com,布布扣     Title
bubuko.com,布布扣
FROM titles
bubuko.com,布布扣
GROUP BY
bubuko.com,布布扣   
CASE
bubuko.com,布布扣       
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣       
WHEN price < 10 THEN Bargain
bubuko.com,布布扣       
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣       
ELSE Gift to impress relatives
bubuko.com,布布扣   
END,
bubuko.com,布布扣     Title
bubuko.com,布布扣
ORDER BY
bubuko.com,布布扣   
CASE
bubuko.com,布布扣       
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣       
WHEN price < 10 THEN Bargain
bubuko.com,布布扣       
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣       
ELSE Gift to impress relatives
bubuko.com,布布扣   
END,
bubuko.com,布布扣     Title
bubuko.com,布布扣
GO
bubuko.com,布布扣
bubuko.com,布布扣注意,为了在
GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。
bubuko.com,布布扣
bubuko.com,布布扣除了选择自定义字段之外,在很多情况下
CASE 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集。

sql case when 用法,布布扣,bubuko.com

sql case when 用法

标签:style   blog   http   color   使用   strong   

原文地址:http://www.cnblogs.com/dullbaby/p/3833209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!