select realname as hname from a, b where b.h_id=a.id
select realname as pname from a,b where b.p_id=a.id
我想把 pname和hname 同时显示,还需要怎么做啊

解决方案 »

  1.   


    select (select realname from b where h_id = a.id) as hname,
           (select realname from b where p_id = a.id) as pname
    from a--或:select t.realname as hname,e.realname as pname
    from a left join b t on a.id = t.h_id
           left join b e on a.id = e.p_id--都不行? 
      

  2.   

    select realname as hname from a, b where b.h_id=a.id
    select realname as pname from a,b where b.p_id=a.id想要什么结果?通过A.ID连接?试试下面的看看哪种是你想要的
    select a.hname,b.pname 
    from
    (select realname as hname,a.id as id from a, b where b.h_id=a.id) a,
    (select realname as pname ,a.id as id from a,b where b.p_id=a.id) b
    where a.id=b.id
      

  3.   

    select 
    (select realname from b where b.h_id=a.id) hname ,
    (select realname from b where b.p_id=a.id) pname
    from a---应该还有其他的关联字段 有的话可以case 来做
      

  4.   

    恩,我开始看的也觉得是对的,select realname as hname from a, b where b.h_id=a.id
    select realname as pname from a,b where b.p_id=a.id查询出的结果数量是不一样的
      

  5.   

    不清楚是不是这个意思select realname as hname,'' as pname from a, b where b.h_id=a.id union all
    select '' as hname,realname as pname from a,b where b.p_id=a.id;
      

  6.   

    --难道是这样?
    select a.hname,b.pname 
    from
    (select realname as hname,a.id as id from a, b where b.h_id=a.id) a,
    (select realname as pname ,a.id as id from a,b where b.p_id=a.id) b
    where a.id(+)=b.id(+)
      

  7.   

    结果集合并不就行了? 楼主还有什么要求?select 'h_id' id_type, realname as hname from a, b where b.h_id=a.id
    union all
    select 'p_id' id_type, realname as pname from a,b where b.p_id=a.id
      

  8.   

    一条select搞定 ,from后表名用别名。
      

  9.   

    用union会把重复的数据去掉 用union all就可以保留重复记录
      

  10.   

    select b.realname hname,c.realname pname from a a, b  b, b c  where a.id=b.h_id and a.id=c.p_id
      

  11.   

    9楼那是用一列,我想用2列。mysql还不支持(+)连接啊
      

  12.   

    我先是分别用2个左外连接去select,但是在合并的时候就是出不来数据