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)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