当前位置: 代码网 > it编程>数据库>Oracle > oracle关联查询报invalid number错误的解决方法

oracle关联查询报invalid number错误的解决方法

2024年09月30日 Oracle 我要评论
出现问题的原因和背景oracle进行关联查询的时候因为字段存在多个用逗号切割的id,导致查询的过程中报无效数字或非法数字问题复现1新建表acreate table "a" ( id number

出现问题的原因和背景

oracle进行关联查询的时候因为字段存在多个用逗号切割的id,导致查询的过程中报无效数字或非法数字

问题复现1

新建表a

create table  "a" (
  id number not null,
  name varchar2(255 byte)
)
 
insert into "a" values ('1', '上海');
insert into "a" values ('2', '北京');
insert into "a" values ('3', '广州');
insert into "a" values ('4', '深圳');

新建表b

 
create table  "b" (
  id number not null,
  aid varchar2(255 byte)
)
 
insert into "b" values ('1', '1,2,3');
insert into "b" values ('2', '1,2');
insert into "b" values ('3', '1,2,3,4');

问题复现2

select * from a where id in (select aid from b where id = 3)

产生的原因:因为查询过程中,多个id放在了一个字段,这个字段不止有数字也有逗号,id是数字类型,所以数据库识别以后直接会报非法数字。

解决方法

先将子查询转成多行

	with 
    split_string as (
		        select   (select aid from b  where id = 3)as string from dual
    )
select 
    regexp_substr(string, '[^,]+', 1, level) as aid
from 
    split_string
connect by
    regexp_substr(string, '[^,]+', 1, level) is not null;

再次查询a表数据

 select * from a  where id  in (	with 
    split_string as (
		        select   (select aid from b  where id = 3)as string from dual
    )
select 
    regexp_substr(string, '[^,]+', 1, level) as aid
from 
    split_string
connect by
    regexp_substr(string, '[^,]+', 1, level) is not null)

将a表的名称转成一行

select listagg(name,',') within group (order by name) 关联流程 from a where  id in (	with 
    split_string as (
        select   (select aid from b  where id = 3)as string from dual
    )
select 
    regexp_substr(string, '[^,]+', 1, level) as split_value
from 
    split_string
connect by
    regexp_substr(string, '[^,]+', 1, level) is not null) 

以上就是oracle关联查询报invalid number错误的解决方法的详细内容,更多关于oracle报invalid number错误的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com