根据不同条件进行不同的update

Id  Total  Name
1   1   a
2   2   b
3   9   dfgdg
4   7   dfd
5   4   cc
6   4   ccv
7   2   cb
8   1   bbb
9   8   cc
10   5   bbb

 

数据库数据如上所示

需求:total大于5,total置为5,total小于3,置为1,其他totao置为3

update A
set total = case when total<3 then 1
   when total>5 then 5
   else 3
   end

 

运行上面这句后的效果是

Id  Total  Name
1   1   a
2   1   b
3   5   dfgdg
4   5   dfd
5   3   cc
6   3   ccv
7   1   cb
8   1   bbb
9   5   cc
10   3   bbb

 

其实就是用case when then

看来不光在查询时候可以用这个,update时候也可以用

 

case when then

在select 时候的用例

select case total when 3 then '正常'
          when 5 then '优秀'
          else '不合格'
          end
          from A

  

posted @ 2010-06-09 11:02  艾伦  阅读(595)  评论(1编辑  收藏  举报