温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

oracle中的case when then怎么使用

发布时间:2023-03-01 17:08:51 来源:亿速云 阅读:69 作者:iii 栏目:开发技术

本篇内容主要讲解“oracle中的case when then怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“oracle中的case when then怎么使用”吧!

关于case when then的使用

1.首先创建两个表emp,emp_bonus如下:

(1)emp_bonus:

>oracle中的case when then怎么使用

(2)emp:

oracle中的case when then怎么使用

2.首先对emp_bonus表进行操作:

select emp_bonus.*,(case when empno=7934 then 0
            when empno=7839 then 1 else -1
           end) as asd from emp_bonus

效果:


oracle中的case when then怎么使用

可见case when then 的作用或者效果是根据条件重新添加了一列!其实这一列可以的多表关联中进行其他有效的操作。

3.如,员工的奖金根据emp_bonus中的type来计算,type=1则工资的10%作为奖金,type=2则工资的20%作为奖金,以此类推……现在要求返回10号部门的员工工资及奖金:

select e.deptno,e.deptno,e.ename,e.sal,
    (e.sal*case 
      when eb.type=1 then 0.1
      when eb.type=2 then 0.2
      when eb.type=3 then 0.3 end) as bonus from emp e inner join emp_bonus eb on(e.empno=eb.empno) and e.deptno=10

oracle中的case when then怎么使用

可见,两表关联后的bonus列进行了操作……

Oracle语句中case when起别名

配合前端框架,前后台查出来的数据是经常配合的,比如一个党员信息列表,针对性别字段,后台查出来的是0或者1代表男和女,一种方式是前台针对0,1进行文字转换,像easyui和layui等都提供相应的js写法。

easyui 在datagrid 的columns:数组里面使用formatter函数实现。

{width : '5%',title : '性别',field : 'sex',sortable : true,formatter:function(value,row,index){
                switch (value) {
                case '1':
                    return '<font >男</font>';
                    break;
                default:
                    return '<font >女</font>';
                    break;
                }
            }},

在layui里可以使用layui模板语法实现。

window.demoTable = table.render({
                                        elem : '#idTest',
                                        id : 'idTest',
                                        url : '<%=path%>/partyMember/getPartyMembersByOrgCode',
                                        width : 1500,
                                        height : 650,
                                        cols : [ [ //标题栏
                                            {checkbox : true,LAY_CHECKED : false,filter : 'test'}, 
                                            {field : 'PM_CODE',title : '党员编号',width : 220,sort : true,align : 'center'}, 
                                            {field : 'NAME',title : '党员姓名',width : 120,sort : true,align : 'center'}, 
                                            {field : 'SEX',title : '性别',width : 80,sort : true,align : 'center',templet:'#sexTpl'}, 
                                            {field : 'MOBILE_NO',title : '手机号码',width : 220,sort : true,align : 'center'}, 
                                            {field : 'CODE',title : '组织编码',width : 220,sort : true,align : 'center'}, 
                                            {field : 'ORG_NAME',title : '所在支部',width : 200,sort : true,align : 'center'}, 
                                            {fixed : 'right',title : '操作',width : 200,align : 'center',toolbar : '#barDemo'} ] ],
                                        page : true //是否显示分页
                                        ,
                                        limits : [ 15, 20,50, 100 ],
                                        limit : 15
                                    //每页默认显示的数量
                                    });

<script type="text/html" id="sexTpl">
{{#  if(d.SEX ==0){ }}
     女
{{#  } else { }}
    男
{{#  } }}
</script>

第二种方法:直接数据库里返回文字信息。

在这种情况下遇到了case when 语句别名的问题,在此博客记录下,希望各位也留意。

刚开始写的语句

select name,case sex when '0' then '男' else '女' end as '性别' from t_member_base where mobile_tel='13707979894'

报如下错误:

ORA-00923: 未找到要求的 FROM 关键字
00923. 00000 -  "FROM keyword not found where expected"
*Cause:    
*Action:
行 1 列 60 出错

查了相关资料才知道,oracle case when 取别名是不能带as的,去掉as即可。

oracle中的case when then怎么使用

而且性别还不能带单引号,一般程序里都是取英文,如果是汉子,还不能带单引号,直接汉子即可,说明下。

到此,相信大家对“oracle中的case when then怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI