2個表  a (id  int, type int,tournameId int)   b (bid, eventid)
其中a表有父子關系,當type=2 tournameId為空  當type=1 ,tournameId為全表里的某個id的值。
如 a
1    2    null
2    1     1
3    1      2
4    2      null
5.............b表的 eventid為第一個表的id的值要求:找出a表中所有type=1的id 并且他們的父親id ,將父子作為一個組合排序
     如上面的
1    2    null
2    1     1
3    1      2
就是一個分組。將所有a表的數據分組之后,找出這些分組全部在b表里面的bid。

解决方案 »

  1.   

    没看懂,也没猜出来你的需求到底是什么? 输出会是什么?建议提供测试数据。父子关系,可以参考一下这个贴子MySQL中进行树状所有子节点的查询 
    http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/02/4142971.aspx
      

  2.   

    可能沒有說清楚
    找出a表中所有type=1的id 并且他們的父親id ,將父子作為一個組合排序 
        如上面的 
    1    2    null 
    2    1    1 
    3    1     15    2    null
    6    1     5
    7    1     5 
    8    1      5
    就是2個分組。 
    可以看出是有孩子節點的id和孩子節點的id組合成一個分組   (2表示可能有孩子加點,當然也可能沒有孩子節點)現在簡化問題,求 有孩子節點的id分組(父親 孩子節點全部) 全部在b表中存在的分組找出來
      

  3.   

    select  id  from a e1 where type=2 and id in (select eventid from b where exists (select id from a e2 where e2.type=1 and e2.tournameId=e1.id ))
      

  4.   

    猜一下了,看不出来表b有什么作用。select *
    from a
    order by if(type=2,id,tournameId),type desc
      

  5.   

    b表中 有孩子節點的id分組(父親 孩子節點全部)  的記錄才選擇出來