oracle clob去重复,Oracle 中对clob字段进行distinct 操作

oracle 10+g不支持对类型为CLOB的列进行distinct,也不支持union,所以在遇到此问题,需要对SQL语句进行重新,从另一思想去实现同样的效果的。union没仔细思考过,具体还要看union里面的条件如何,最简单的方法是利用to_char将clob字段转换成char型,但存在一个问题,如clob中的字符长度超过4000时会报错。在此主要对distinct的转换方法进行列举:

表结构如下:

Table1:

id(varchar) title(varchar) content(clob)

1 title1

2 title2

Table2:

id(varchar) table_id(varchar) object(varchar)

1 1 123

2 1 abc

3 1 111

4 1 222

5 2 123

SQL语句:

select distinct table1.* from table2 table2 left join table1 table1 on table1.id = talbe2.table_id

where table2.object = '123' or table2.object = '111' or table2.object = '222' or table2.object = '333'

执行该语句,出现错误:ORA-00932,数据类型不一致:应为-,却获得CLOB。

1、利用rowid,过滤重复的数据。

这个方法的思路来源,就是对某个字段进行distinct时的解决方法。

基本思路:select B.name,B.id from table B where not exists (select 1 from table A where A.name = B.name AND B.id_key > A.id_key)

A表,B表为同一张表,表中的数据存在name相同的,但id_key不同,该语句的目的是把同名的只显示一条即可,其它同名记录过滤。

将该思想转换到当前的问题上,利用每条记录的rowid不同进行过滤。

select p4.*, p4.rowid from (

select p.* from table2 s left join table1 p on s.table_id = p.id

where s1.object = '123' or s1.object = '111' or s1.object = '222' or s1.object = '333'

) p4

where not exists (

select 1, p2.rowid from (

select p1.* from table2 s1 left join table1 p1 on s1.table_id= p1.id

where s1.object = '123' or s1.object = '111' or s1.object = '222' or s1.object = '333') p2

where p4.id = p2.id and p4.rowid > p2.rowid

)

2、采用子查询进行过滤重复的数据。

select * from table1 t1 where t1.id in (

selcet distinct p1.id from table2 s1 left join table1 p1 on s1.table_id= p1.id

where s1.object = '123' or s1.object = '111' or s1.object = '222' or s1.object = '333'

)

3、采用to_char函数,直接使用distinct(前提是:clob字段中的字符长度少于4000)。

select distinct table1.id, table1.tittle, to_char(table1.content) from table2 table2

left join table1 table1 on table1.id = talbe2.table_id

where table2.object = '123' or table2.object = '111' or table2.object = '222' or table2.object = '333'

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值