mysql篇-mysql与orcale函数使用对比

本文主要是记录项目开发中oracle遇到的问题,最近做mysql迁移到oracle,发现还是挺多函数无法在oracle中使用的,

listagg聚合函数

mysql中可以使用group_concat()函数来讲多行数据合并成一个数值,以逗号隔开,但是在oracle中无法使用,oracle中想要实现这种方式貌似有两种,一种是vm_concat(),但是这种方式好像不稳定,与oracle版本关系很大,不太敢使用,毕竟生产和测试的数据库版本不敢保证一致。使用了另一种方式:

listagg(字段名,'分隔符') within group (order by 排序字段)

select DISTINCT nm,listagg(val,',') within group (order by u_id) as val,listagg(u_id,',') within group (order by u_id) as u_id from table group by nm;

记录下遇到的报错:

ORA-00904: "WM_CONCAT": invalid identifier

listagg 没有配合group by使用报错:

ORA-00937: not a single-group group function

直译的话,不是单一的分组,毕竟没有分组嘛,所以无法聚合起来。

select DISTINCT nm,listagg(val,',') within group  from table group by nm;

listagg(val,‘,’) within group 没有使用后面的order by 也是会报错:

ORA-00906: missing left parenthesis
所以聚合函数完整的使用:
select DISTINCT nm,listagg(val,',') within group (order by u_id) as val,listagg(u_id,',') within group (order by u_id) as u_id from table group by nm;

模糊查询

mysql使用like 可以在后面接上concat(‘%’,‘’,‘%’) ,oracle不能这样使用,正确写法:

like  '%' || 参数 || '%'

批量导入

集成mybatis的批量导入,使用一个List的数据结构作为传参,mybatis采用foreach标签

<insert id="batchSql" parameterType="java.util.List">
		INSERT INTO table  (name,age)
		 <foreach collection ="list" item="item" index="index" separator ="UNION ALL">
		 	select #{item.name},#{item.age} from dual
		 </foreach>
	</insert>

java处理数据

List<Map> mpParam = new ArrayList<Map>();
Map mpMap=new HashMap();
mpMap.put("name","zhangsan" );
mpMap.put("age","12" );
mpParam.add(mpMap);
Map mpMap1=new HashMap();
mpMap1.put("name","lisi" );
mpMap1.put("age","20" );
mpParam.add(mpMap1);
// 调用mapper的接口方法

多个数值拼接

mysql拼接字符串可以使用concat,但是在oracle中只能使用||,比如

// 两个数值拼接
select 'a'||'b' from dual

在这里插入图片描述
字符串之间有其他隔开,写法:

select 'a'||':'||'b' as params from dual

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值