表结构是这样的 id 主键 pguid 另外一个表的外键
id pguid   catalog      value
1    1    0500001       zhangsan
2    1    0500003        lipeng
2    1    0500004        lipeng
2    1    0500004        lipeng
3    2    0500001         wddsfd
....
66    43    0500006         wddsfd
.......
我要查的是 pguid=1的记录中的catalog=0500001 的value的值 和catalog=0500002的value值,假如catalog中没有该值则返回"无" ,应该怎么写这条sql语句 如select nvl(value1,'无'),nvl(value2,'无') from (...) 查询结果:
value1     value2
zhangsan    无
特急,请知道的朋友告诉我
       

解决方案 »

  1.   

    用LEFT JOIN 就行了。select a.pguid,
    nvl(b.value,'无'),
    nvl(c.value,'无')
    from 
    ((select 1 as pguid from dual) a
    left join (select value from 表 where catalog=0500001) b on a.pguid=b.pguid)
    left join (select value from 表 where catalog=0500002) c on a.pguid=c.pguid
        [align=center]====  ====
    [/align]
      

  2.   

    楼上写的代码本身有错啊 ,不能执行,还有就是catalog中可能没有我输入的条件啊
      

  3.   

    你的SQL语句是什么? 出错提示是什么?
        [align=center]====  ====
    [/align]
      

  4.   

    select nvl(value,'无') value from test where pguid=1 and catalog in ('0500001','0500002');
      

  5.   

    SQL code    select a.pguid, nvl(b.value,'无'), nvl(c.value,'无') from test a LEFT OUTER JOIN (select value from test where catalog='0500001' and pguid=1 ) b on a.pguid=b.pguid LEFT OUTER JOIN  (select value from test where catalog='0500002'and pguid=1 ) c on a.pguid=c.pguid
    t where a.pguid=1 仅供参考.