ORACLE实现MySQL中find_in_set同名函数功能,oracle使用find_in_set函数,oracle集成mybatis使用find_in_set函数

ORACLE实现MySQL中find_in_set同名函数功能,oracle使用find_in_set函数,oracle集成mybatis使用find_in_set函数

话不多说直接上案例:

比如一张表:
  sys_dept(dept_id, parent_id, ancestors,……);

实例:

在这里插入图片描述

MySQL语法:

select * from sys_dept where find_in_set('100',ancestors);

Oracle方法1:

-- ----------------------------
-- 函数 ,代替mysql的find_in_set
-- 例如: select * from sys_dept where FIND_IN_SET (101,ancestors) <> 0
-- mysql可接受0或其它number做为where 条件,oracle只接受表达式做为where 条件
-- ----------------------------
create or replace function find_in_set(arg1 in varchar2,arg2 in varchar)
return number is Result number;
begin
select instr(','||arg2||',' , ','||arg1||',') into Result from dual;
return(Result);
end find_in_set;

博主这里创建了还是报错:ORA-00920: 无效的关系运算符
在这里插入图片描述

博主的11g版本数据库不支持,提示ORA-00920: 无效的关系运算符,于是研究了另一个方法:

如果方法1无效可以直接使用instr函数替换find_in_set,满足需求。

select * from sys_dept where instr(','||ANCESTORS||',' , ','||100||',')<>0 ;

在mybatis中

<select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
		select * from sys_dept where instr(','||ancestors||',' , ','||#{deptId}||',') <![CDATA[ <> ]]> 0
</select>

<![CDATA[ <> ]]> 0是用于mybatis对"<>"进行转义操作的。

instr( string1, string2, start_position,nth_appearance )

●string1:源字符串,要在此字符串中查找。
●string2:要在string1中查找的字符串 。
●start_position:代表string1 的哪个位置开始查找。此参数可选,如果省略默认为1.
字符串索引从1开始。如果此参数为正,从左到右开始检索,如果此参数为负,从右到左检索,返回要查找的字符串在源字符串中的开始索引。
●nth_appearance:代表要查找第几次出现的string2. 此参数可选,如果省略,默认为 1.如果为负数系统会报错。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小诺大人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值