aTable  
 
person    ¦  key  
----------------  
a                            3  
a                            4  
b                            5  
b                            3  
b                            4  
c                            3    
c                            4  
 
要做的是,在表中查询拿有相同钥匙的人  
我这样做怎么老是运行不了  
 
select  p1.person,p2.person  from  aTable  p1,  aTable  p2  
having  
(  
select  ptemp1.key  from  aTable  ptemp1  
where  ptemp1.person  =  p1.person  
and  exists  
     (  
       select  key  from  aTable  
             where  person  =  p2.person  
     )    
)  
and  
(  
select  ptemp2.key  from  aTable  ptemp2  
where  ptemp2.person  =  p1.person  
and  exists  
     (  
       select  key  from  aTable  
             where  person  =  p2.person  
     )    
)

解决方案 »

  1.   

    希望输出
    person1 | person2
    -------- --------------
     a             c因为这两个用有相同的key
      

  2.   

    不行,你就干脆把他们读到datatable中,然后对rows数组进行处理吧。呵呵~
      

  3.   

    select  p1.person,p2.person from  aTable  p1,  aTable  p2  
    where p1.key = p2.key
    不就好了?
      

  4.   

    select t1.person,t2.p2 person
    from (select person,count(distinct key) key from a group by person) t1 inner join (select a1.person p1,count(distinct a1.key) key1,a2.person p2,count(distinct a2.key) key2
    from a a1 left join a a2 on a1.key = a2.key
    where a1.person <> a2.person
    group by a1.person,a2.person) t2 on t1.person = t2.p1 and t1.key = t2.key1
    order by 1,2SQL> select * from a;PERSON                                     KEY
    ------ ---------------------------------------
    a                                            3
    a                                            4
    b                                            3
    b                                            5
    c                                            3
    c                                            4
    d                                            3
    d                                            4结果:
    PERSON PERSON
    ------ ------
    a      c
    a      d
    c      a
    c      d
    d      a
    d      c6 rows selected只能做到这程度了!
      

  5.   

    select distinct a.person,b.person,a.key from atable a,atable b where a.key=b.key
      

  6.   

    select distinct a.person,b.person,a.key from atable a,atable b where a.key=b.keyboyofcity(boyofcity的这一查询语就可以解决了啊