您的位置:首页 > 数据库 > Oracle

Oracle--(case when then else end )和(update set replace)的用法

2016-04-14 17:50 453 查看
update `表名` set 字段名 =replace(字段名, '查找的内容','更改的内容') where 字段名 like '%查找的内容%';

update shangpin set click_url=replace(click_url,'21508360','1111111111') where click_url like '%21508360%';

--------------------------------------------------------------------------------------------------

SELECT a.managecom,
a.subtype,
count(*) loadsucc,
sum(case when a.state in  ('4', '5', '6', '7', '8', '9') then 1 else 0 end) recogsucc,
sum(case when a.state in  ('3', '12', '13') then 1 else 0 end) recogfail,
sum(case when a.state in  ('1', '2') then 1 else 0 end) waitrecog
FROM ocr_docdetail a, ocr_loaddetail c
WHERE 1 = 1
and a.managecom like '86%'
and a.managecom = c.managecom
and a.bussno = c.bussno
and a.subtype = c.subtype
and c.loadstate = 0
and c.scandate >= date '2012-07-29'
and c.scandate <= date '2013-01-07'
group by a.managecom, a.subtype
order by a.managecom, a.subtype;

case具有两种格式。简单case函数和case搜索函数。
--简单case函数
case sex
when
'1' then '男'
when
'2' then '女'
else
'其他' end
--case搜索函数
case
when sex = '1'
then '男'
when sex =
'2' then '女'
else
'其他' end

这两种方式,可以实现相同的功能。简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式。

还有一个需要注重的问题,case函数只返回第一个符合条件的值,剩下的case部分将会被自动忽略。
--比如说,下面这段sql,你永远无法得到“第二类”这个结果
case
when col_1 in (
'a', 'b') then'第一类'
when col_1
in ('a')
then '第二类'
else'其他'end
原文:http://blog.csdn.net/xuxurui007/article/details/8479953
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: