ThreadID     ParentID   ThreadAuthor
  05         05           wgh
  06            05        有痔青年
  07            05          abc
  08            05          abc
  09            09          zxw 
  10            09          ll
  11        09          abc这是bbs的贴子表结构 ThreadID与ParentID相等的为楼主发的贴,不相等的为跟贴,
现在我想查询用户abc跟贴的所有楼主贴 ,也就是ThreadID为05,09的贴,用SQl语句如何写

解决方案 »

  1.   

    select distinct parentid from table where threadauthor = 'abc' and treadid <> parentid
      

  2.   

    select * from table x inner join (
      select distinct parentid from table where threadauthor = 'abc' and   treadid=parentid
    ) y on x.ThreadID=y.parentid
      

  3.   

    改正下select * from table x inner join (
      select distinct parentid from table where threadauthor = 'abc' and   treadid<>parentid
    ) y on x.ThreadID=y.parentid
      

  4.   

    to:asen51(AS阿森51)
    能具体解释一下x,y吗?谢了,
      

  5.   

    我现是在一个表中操作,比如表名为Threads,并没有表x,y
      

  6.   

    SELECT * FROM TBL WHERE ParentID IN (
    SELECT ParentID FROM TBL WHERE ThreadAuthor='aaa')
      

  7.   

    SELECT * FROM TBL WHERE ParentID=ThreadID AND ParentID IN (
    SELECT ParentID FROM TBL WHERE ThreadAuthor='aaa')