在Oralce中有个函数为Decode(),感觉非常好用,但是此函数为Oracle所独有,在SQLSERVER和MYSQL中实现与之相类似的功能还无相关函数,但通过CASE关键词可实现类似功能。
1 》》首先让我们看一下 CASE 的语法。在一般的 SELECT 中,其语法如下:
SELECT =
CASE
WHEN THEN
WHEN THEN
ELSE
END
2 》》 我在SQLSERVER2000的实践
表t_notice有字段result为INT类型,有三种值:0表示发送成功,1表示发送失败,-1表示未发送,需查询出RESULT的值并返回数值所代表的中文。
SELECT count(*), "result"=CASE //单引号内的result为别名
WHEN result=0 THEN "发送成功"
WHEN result=1 THEN "发送失败"
WHEN result=-1 THEN "未发送"
END
FROM t_notice
ORDER BY result
3 》》用Hibernate做持久化,HQL的写法
SELECT count(*), CASE WHEN result=0 THEN "发送成功"
WHEN result=1 THEN "发送失败"
WHEN result=-1 THEN "未发送"
END
FROM TNotice
ORDER BY result
注: TNotice为持久化类类名,HQL用CASE时,无需为查询字段加别名。
4 》》以上是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 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
你甚至还可以组合这些选项,添加一个 ORDER BY 子句,如下所示:
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
注意,为了在 GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。
除了选择自定义字段之外,在很多情况下 CASE 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集
1 》》首先让我们看一下 CASE 的语法。在一般的 SELECT 中,其语法如下:
SELECT =
CASE
WHEN THEN
WHEN
ELSE
END
2 》》 我在SQLSERVER2000的实践
表t_notice有字段result为INT类型,有三种值:0表示发送成功,1表示发送失败,-1表示未发送,需查询出RESULT的值并返回数值所代表的中文。
SELECT count(*), "result"=CASE //单引号内的result为别名
WHEN result=0 THEN "发送成功"
WHEN result=1 THEN "发送失败"
WHEN result=-1 THEN "未发送"
END
FROM t_notice
ORDER BY result
3 》》用Hibernate做持久化,HQL的写法
SELECT count(*), CASE WHEN result=0 THEN "发送成功"
WHEN result=1 THEN "发送失败"
WHEN result=-1 THEN "未发送"
END
FROM TNotice
ORDER BY result
注: TNotice为持久化类类名,HQL用CASE时,无需为查询字段加别名。
4 》》以上是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 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
你甚至还可以组合这些选项,添加一个 ORDER BY 子句,如下所示:
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
注意,为了在 GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。
除了选择自定义字段之外,在很多情况下 CASE 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集