本文主要是记录项目开发中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
<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