一个表的字段有id,name,parent_id(父亲的id),查找做爷爷的人!~
写一个SQL语句.

解决方案 »

  1.   

    try:select * from (
    select * from table1 where id in(select parent_id from table1)) where id in (
    select parent_id from table1 where id in(select parent_id from table1) );
      

  2.   

    select B.id
    from 表名 A,表名 B
    where A.parent_id = B.id
      

  3.   

    上面按错了select B.parent_id 
    from 表名 A,表名 B
    where A.parent_id = B.id
      

  4.   

    试试这个。。
    select * from tablename where id in(select distinct parent_id from table where id in(select distinct parent_id from tablename))
      

  5.   

    针对这种情况,db2中有with,oracle中有start with
    你不会以为IBM和oracle都疯了,没有用的东西也加到数据库上?
      

  6.   

    呵呵,很有意思,看看我这个select id,name from table_name a
     where exists (select id from table_name b 
                    where b.parent_id = a.id
                      and exists (select id from table_name c where c.parent_id = b.id));
      

  7.   


    IBM内部的项目,很多在数据库中用的就是这种存储方式,我看你真不用说了。
      

  8.   

    select t.id,t.name from table t where  t.id in(select count(*) as num from table a group by parrentid where num>2)  这个行不 
      

  9.   

    select t1.* from table1 t1,
    (select ParentID from table1 t2 where exists(select t3.ID from table1 t3 where t3.ParentId=t2.ID)) t4
    where t1.ID=t4.ParentID;
      

  10.   

    select a.id,a.name from table a,table b,table c where a.id=b.parent_id and b.id=c.parent_id;
      

  11.   

    select t1.parent_id from table t1 where t1.id in (select t2.parent_id from table t2 where t2.id is not null) 
    这样对么?
      

  12.   

    问题很简单。
    这个在IBM面试里面应该不算是什么复杂问题。