现有表A,其中表A的结构如下
id code name
1 A 语文
2 B 数学
3 C 英语表B的结构如下
product_id product_code
1 A,B
2 A
3 A,B,C我现在想要查询表B的数据,并且把product_code转换成对应的中文,如
1 语文,数学
2 语文
3 语文,数学,英语我的sql是这样的:
select B.id,( select to_char(wm_concat( name )) as menu_name from A where id in
( select product_code from B where id =B.id ) ) as menu_name from B B
这样只有当product_code为A的时候才正确显示,求指导oracle 字符串 转换
id code name
1 A 语文
2 B 数学
3 C 英语表B的结构如下
product_id product_code
1 A,B
2 A
3 A,B,C我现在想要查询表B的数据,并且把product_code转换成对应的中文,如
1 语文,数学
2 语文
3 语文,数学,英语我的sql是这样的:
select B.id,( select to_char(wm_concat( name )) as menu_name from A where id in
( select product_code from B where id =B.id ) ) as menu_name from B B
这样只有当product_code为A的时候才正确显示,求指导oracle 字符串 转换
SCOTT@xp-ora10g> select * from A; ID CODE NAME
---------- ----- ----------
1 A 语文
2 B 数学
3 C 英语SCOTT@xp-ora10g> select * from B;PRODUCT_ID PRODUCT_CO
---------- ----------
1 A,B
2 A
3 A,B,CSCOTT@xp-ora10g> select product_id,product_code,decode(product_code,
2 'A,B','语文,数学',
3 'A','语文',
4 '语文,数学,英语')
5 from A,B where id=product_id;PRODUCT_ID PRODUCT_CO DECODE(PRODUCT_CODE,'A,B','语文,数学','A
---------- ---------- ----------------------------------------
1 A,B 语文,数学
2 A 语文
3 A,B,C 语文,数学,英语
大哥能不能高级点,因为我存的是主键ID,会变动的,so 你懂得
或者自己写个类似wm_concat的也行。
case when
case 截取B.product_code非逗号前一位或者最后一位=A.code 显示A.name
case ……
end
from b中间具体自己琢磨吧,不过此语句速度可能要慢。