首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

oracle求解,该怎么处理

2012-12-26 
oracle求解select * from ItemTablewhere item_id in (1,2,3,4,5,6,7,8,9) 上面是需求select * from itemt

oracle求解
select * from ItemTable  where item_id in (1,2,3,4,5,6,7,8,9) 
上面是需求


select * from itemtable where item_id in 
(
select pid from roletable where  rid in 
(
select rid from usertable where uid=1
)
)

我的数据表里的一列是 1,2,3,4,5,6,7,8,9  varchar类型的。


select pid from roletable where  rid in 
(
select rid from usertable where uid=1
) 查出来的数据是 1,2,3,4,5,6,7,8,9  varchar类型的。

select * from itemtable where item_id in 
(

)item_id 是number类型的 

求解:怎么把1,2,3,4,5,6,7,8,9  varchar类型的 变成 最上面那种id列表。
[解决办法]
1,2,3,4,5,6,7,8,9 是存储在一个字段里面的么?
如果是的话
select * from itemtable where to_char(item_id) in 
(

)item_id 是number类型的 
如果每个字段存一个,如:1 或者2 或者 3 
如下
select * from itemtable where item_id in 
(
select to_number(rid) from usertable where uid=1

[解决办法]

SELECT REGEXP_SUBSTR('1,2,3,4,5,6,7,8,9','\d+',1,rownum)AS  item_id from dual connect by level <=SUBSTR('1,2,3,4,5,6,7,8,9',-1,1)


   ITEM_ID
11
22
33
44
55
66
77
88
99
[解决办法]
select * from itemtable where item_id in 
 (
 select to_number(pid) from roletable where  rid in 
 (
 select rid from usertable where uid=1
 )
 )
[解决办法]
--分割逗号
with t1 as 
(
     select 1 c1,'abc,def,mns,opq' c2 from dual
     union all
     select 2 c1,'a,b' c2 from dual
)

select distinct c1,replace(regexp_substr(c2,'[^,]+',1,level),',',' ') c2
from t1 
connect by level<=length(c2)-length(replace(c2,',',''))+1
order by c1


     c1     c2
-------------------------
11abc
21def
31mns
41opq
52a
62b
[解决办法]
把item_id转成字符串,前后加上逗号,结果为A
然后把select pid from roletable where  rid in 
(
select rid from usertable where uid=1
) 查出来的数据前后也加上逗号结果为B,
然后在B中查找是否存在子串A,使用instr函数,如果没有找到返回0否则返回>0
例如item_id是2, A=',2,'
B=',1,2,3,4,5,6,7,8,9,',
返回大于0;

例如item_id是2, A=',2,'
B=',1,3,4,5,6,7,8,9,20,',
返回0。前后加上逗号就是为了防止这里返回>0。

scott@ORA11GR2> select instr(',1,2,3,4,5,6,7,8,9,', ',2,') from dual;

INSTR(',1,2,3,4,5,6,7,8,9,',',2,')
----------------------------------
                                 3



scott@ORA11GR2> select instr(',1,3,4,5,6,7,8,9,20,', ',2,') from dual;

INSTR(',1,3,4,5,6,7,8,9,20,',',2,')
-----------------------------------
                                  0



所以你的查询可以改成
select * from itemtable where instr(
','

热点排行