欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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

程序员文章站 2022-05-28 13:57:23
...

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
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
FROM titles
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
ORDER BY price
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
GO

举例:

数据库pubs

表titles

Title price

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
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 Title
SQL:select case 用法详解  带例子 图解说明    sqlserver2000
ORDER 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 Title
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