现有两个表 
表一 
lxid  name 
01    红文具盒 
02    铅笔 
表二 
id    lxid  upid 
1    01 
2    02  01 
3    02  01 
这两个表的意思是 
在文具盒中放了2只铅笔, 
现在想得到如下结果
比如 
id  lxid    name      upname
1    01    红文具盒 
2    02    铅笔         红文具盒
3    02    铅笔         红文具盒

解决方案 »

  1.   

    select c.id,c.lxid,c.name,a.name upname from (select b.id,b.lxid,a.name,b.upid from a,b
    where a.lxid=b.lxid) c,a
    where c.lxid=b.lxid
      

  2.   

    create table t_06(lxid varchar2(10),name varchar2(10));
    create table t_07(id int,lxid varchar2(10),upid varchar2(10));
    insert into t_06 values('01','红文具盒');
    insert into t_06 values('02','铅笔');
    insert into t_07 values(1,'01',null);
    insert into t_07 values(2,'02','01');
    insert into t_07 values(3,'02','01');
    select a.id,a.lxid,b.name,c.name from t_07 a,t_06 b,t_06 c
    where a.lxid=b.lxid
    and a.upid=c.lxid(+)
    order by id 1 01 红文具盒
    2 02 铅笔 红文具盒
    3 02 铅笔 红文具盒
      

  3.   

    select c.id,c.lxid,c.name,a.name upname from (select b.id,b.lxid,a.name,b.upid from a,b 
    where a.lxid=b.lxid) c,a 
    where c.upid=a.lxid(+) 
    上面写错了
      

  4.   

    select b.id,b.lxid,
    (select name from a where lxid=b.lxid) as name,
    (select name from a where lxid=b.upid) as upname
    from b