例如A条记录的id为1,otherID为2
B记录的id为2,otherID为1,
怎么同时将这2条记录查询出来,就这2个字段的数据的关联的
我也不会,帮你顶一下吧

解决方案 »

  1.   

    select * from tb t1 where exists(select 1 from tb t2 where t1.id=t2.otherID and t1.otherID=t2.id)
      

  2.   

    那就再嵌套一层
    select id,otherID from(select *,groups=(case when t1.id<t1.otherID then rtrim(t1.id)+','+rtrim(t1.otherID) else
     rtrim(t1.otherID)+','+rtrim(t1.id) end) from tb t1 where exists(select 1 from tb t2 where t1.id=t2.otherID and t1.otherID=t2.id) 
    )t  order by groups
      

  3.   

    那就再嵌套一层
    select id,otherID from(select *,groups=(case when t1.id<t1.otherID then rtrim(t1.id)+','+rtrim(t1.otherID) else
     rtrim(t1.otherID)+','+rtrim(t1.id) end) from tb t1 where exists(select 1 from tb t2 where t1.id=t2.otherID and t1.otherID=t2.id) 
    )t  order by groups这个groups提示没这一行
      

  4.   

    我这没问题啊。还有个思路:
    select * from tb t1 where t1.id<t1.otherID  and  exists(select 1 from tb t2 where t1.id=t2.otherID and t1.otherID=t2.id)
    查出来的都是id<otherid的那一条,然后java代码中循环遍历的时候add两条就可以
    add(id,otherid)
    add(otherid,id)
      

  5.   

    我这没问题啊。还有个思路:
    select * from tb t1 where t1.id<t1.otherID  and  exists(select 1 from tb t2 where t1.id=t2.otherID and t1.otherID=t2.id)
    查出来的都是id<otherid的那一条,然后java代码中循环遍历的时候add两条就可以
    add(id,otherid)
    add(otherid,id)
    恩,好的,谢谢了哈