首先传一个ID=1001去数据库 查询 PID=1001的所有记录
然后把查询出来的所有记录ID当成PID继续查询符合条件的记录这是MYSQL语句 不过查不到记录
select  ftm_name from functionitem  where ftm_pid=(select  ftm_ftm_id  from functionitem where ftm_pid=1001)反正就是这样的意思 我怎么实现呢

解决方案 »

  1.   

    select ftm_name from functionitem where ftm_pidin(select ftm_ftm_id from functionitem where ftm_pid=1001)用in试试呢?
      

  2.   

    不知道 数据库结构什么样
    ftm_ftm_id   这个是里面选出来的字段
    而外面 where ftm_pid =
    id 和  pid 有相等的数据吗?
      

  3.   


    是的 他们是级联的关系二楼指出了我SQL语句的错误,但是不想用语句实现,想用程序实现,该怎么做呢 
    说的就是这个意思
      

  4.   

    List list=ftmservice.getftmtwo(myconn,"1001");

    while(list.size()>0){
    for(int i=0;i<list.size();i++){
    ftm=(Functionitem)list.get(i);
    list=ftmservice.getftmtwo(myconn,ftm.getFtm_ftm_id());
    }
    }
      

  5.   

    你这岂不是 一个级联查询啊第一次查出的 id 必须作为参数 并且是循环 id是动态 变动的来执行第二次查询,若果你的id值不是规律 有序的。
      

  6.   

    select a.ftm_ftm_id ,a.ftm_name ,b.ftm_ftm_id ,b.ftm_name from functionitem a left join functionitem b on b.ftm_pid = a.ftm_ftm_id where ftm_pid=1001
    这种写法就行,如果你想取不同a,b表的不同记录,就把前面的字段加上(as 名称) 就行了
      

  7.   

    select a.ftm_ftm_id ,a.ftm_name ,b.ftm_ftm_id ,b.ftm_name from functionitem a left join functionitem b on b.ftm_pid = a.ftm_ftm_id where a.ftm_pid=1001
    这种写法就行,如果你想取不同a,b表的不同记录,就把前面的字段加上(as 名称) 就行了,上帖拉了个a,哈哈
      

  8.   

    一会儿说是想用程序不用SQL语句,那你的数据又从哪来呢?一会儿又说SQL语句没有返回结果,先分开查看有结果没,有的话就是多个查询连接有误
      

  9.   

    这个意思
    =============================
    ID name PID
    ========================
    1001 张山1 1000
    1001 张山2 1000
    1002 张山3 1000
    1003 张山4 1000
    1004 张山5 100010011 张山6 1002
    10012 张山7 1002
    10013 张山8 1002
    10014 张山9 100210015 张山10 1003
    10016 张山14 1003
    10017 张山15 1003
    10018 张山16 100310019 张山17 10014
    10020 张山17 10014
    10021 张山18 10014
    10022 张山19 1001410023 张山19 10019
    10024 张山19 10019
    10025 张山19 1001910026 张山19 10018
    10027 张山19 10018==========================================
    我传一个PID=1000 查询出pid=1000的记录后
    1001 张山1 1000
    1001 张山2 1000
    1002 张山3 1000
    1003 张山4 1000
    1004 张山5 1000=======================
    把它们的ID 1001,1002,1003当成PID继续查询
    查询PID=1001,1002,1003,如此类推,代码怎么实现..................
      

  10.   


    List list1 = new ArrayList();
    //List list2 = new ArrayList();
    Map map = new HashMap();
    String PID = "",ID = "";
    public void testList(){
    sql = "select ID from table where PID= "+PID; 
    ps = con.prepareStatement(sql);
    list1 = ps.executeQuery();
    for(int i=0; i<list1.size();i++){
      sql = "select * from tabel where ID=" + list1.get(i);
      ps = con.prepareStatement(sql);
      //list2 = ps.executeQuery();
      map.put(list1.get(i),ps.executeQuery());
    }
    }
    public void testMap(String ID)
    for(Iterator it = map.entrySet().iterator();it.hasNext();){   
        Map.Entry entry = (Map.Entry)it.next();   
        Object key = entry.getKey();   
        Object val = entry.getValue();  
        if(key.equals(ID) )
        {
          System.out.println(val);  //此时就是为"ID="+ ID的那条数据啦
        }
     }
    }
    //没有经过实验,我想应该可以吧。有错误大家给指出来。。