请教较复杂的SQL语句??谢谢

[复制链接]
查看11 | 回复8 | 2007-10-20 08:38:44 | 显示全部楼层 |阅读模式
a,b字段是表1的数据结构
c是表2的数据结构
a b
c
-----------------
-----
1 非GMP2,10
2 GMP
2,10
10单独定价 2,10
0 其它2,10
以下SQL语句得出上面的数据
selecttab1.a,tab1.c,tab2.cfromtab1,tab2where
instr(tab2.c,tab1.a)>0
andtab2.id=100(产品的ID,举个例子) c的值是2,10。表1的 a 有多个值满足 c ,如 a 的 2,10这两条记录。
求:如何写SQL语句得出以下数据列表
b
c
----------------------------------------------
GMP,单独定价2,10
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
select tab1.a,tab1.c,tab2.c from tab1,tab2 where tab2.id = 100 and tab1.a in
(substr(tab2.c,1,instr(tab2,',')),substr(tab2,instr(tab2,',')+1,length(tab2));
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
请问如何解
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
为何只用表名而没有字段名
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
[php]select t1.c,t2.c1||','||t2.c2
from (
select substr(c,1,1) c1,substr(c,3) c2 from tab2
where instr(c,a) > 0
and id = 100) t2,
(select a,c from tab1 ) t1
where t1.a=t2.c1 or t1.a=t2.c2;[/php]
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
没测试,有错误的话你就模仿着写,应该是对的!
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
哦,你要求的是一行!没看清楚!再看一下!
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
好恐怖啊!改了半天!! 8i以上可以!8i的话没试!!!
[php]SQL> select * from t1;
A B
C
---------- -------------------- --------------------
1 非GMP
1,10
2 GMP
2,10
10 单独定价
2,10
0 其他
2,10
SQL> select rn1||','||rn2 b,c
2from (
3select max(decode(rn,1,b,null)) rn1,max(decode(rn,2,b,null)) rn2,c
4from (
5select b,c,row_number() over(partition by c order by b) rn
6from (
7select b,c1||','||c2 c
8from
9(select substr(c,1,1) c1,substr(c,3) c2 from (select distinct c from t1) ) t1,
10(select a,b from t1) t2
11where t2.a =t1.c1 or t2.a=t1.c2))
12group by c);
B
C
-------------------- --------------------
单独定价,非GMP 1,10
GMP,单独定价 2,10[/php]
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
nmgzw,非常感谢你的解答。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行