大家好,欢迎来到IT知识分享网。
例如,下面的语句显示中文年月
select getdate() as 日期,case month(getdate())
when 11 then ‘十一’
when 12 then ‘十二’
else substring(‘一二三四五六七八九十’, month(getdate()),1)
end+’月’ as 月份
=================================================
CASE 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用 CASE。
首先让我们看一下 CASE 的语法。在一般的 SELECT 中,其语法如下:
SELECT <myColumnSpec> =
CASE
WHEN <A> THEN <somethingA>
WHEN <B> THEN <somethingB>
ELSE <somethingE>
END
在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子:
USE pubs
GO
SELECT
Title,
‘Price Range’ =
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
FROM titles
ORDER BY price
GO
这是 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
GO
你甚至还可以组合这些选项,添加一个 ORDER BY 子句,如下所示:
USE pubs
GO
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
GO
注意:为了在 GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。
除了选择自定义字段之外,在很多情况下 CASE 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集。
7、学号 姓名 语文 数学 英语 总分 平均分
select stuid as 学号,name as 姓名,
sum(case when subject=’语文’ then score else 0 end) as 语文,
sum(case when subject=’数学’ then score else 0 end) as 数学,
sum(case when subject=’英语’ then score else 0 end) as 英语,
sum(score) as 总分,(sum(score)/count(*)) as 平均分
from stuscore
group by stuid,name
order by 总分desc
8、常规 group by 查询
select
'grade'= case
when grade=0 then 'special'
when grade=1 then 'one'
when grade=2 then 'two'
when grade=3 then 'three'
end,
count(grade) total
from trans_see_doctor
where department_code=1
group by grade
9、group by 之后 行转列
select department_code,
sum(case when grade=0 then 1 else 0 end)special,
sum(case when grade=1 then 1 else 0 end)one,
sum(case when grade=2 then 1 else 0 end)two,
sum(case when grade=3 then 1 else 0 end)three
from trans_see_doctor
where 1=1 --and department_code=1
group by department_code
10、统计个数(mysql)
SELECT student_id,
SUM(CASE WHEN is_select = 0 OR is_select IS NULL THEN 1 ELSE 0 END) AS un_selected,
SUM(CASE WHEN is_select != 0 THEN 1 ELSE 0 END) AS selectd
FROM base_photo
WHERE created_time > '2019-09-03 00:00:01' and created_time < '2019-09-03 23:59:59'
GROUP BY student_id;
11、update
update users set
password =
case
when age = 10 then '123456'
when age = 20 then '654321'
else '111111'
end
where id = 1
*
*
*
*
*
*
*
3
2
3
2
3
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23981.html