oracle中关于case when then的使用

2023-12-04 0 262
目录
  • 关于case when then的使用
  • Oracle语句中case when起别名
  • 总结

关于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的使用

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠久资源。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Oracle oracle中关于case when then的使用 https://www.u-9.cn/database/oracle/67307.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务