有sql 和存储过程  sql ,存储过程 

解决方案 »

  1.   

    我是新手 能把SQL语句写出来吗??谢了啊!!
      

  2.   

    最简单的就是增加一列 code
    id         code                 pid
    1          001                  0
    2          002                  0
    3          001001               1
    4          001002               1
    5          002001               2
    6          002001001            5比如查id为6的根菜单  只要取code=前3位就行
      

  3.   

    只用一句SQL是办不到的,有个办法是在程序中写个递归的方法,方法参数是你想要查询的节点和一个List,然后方法里根据此节点去查父节点,当查出来的父节点不是“0”就把父节点放入参数里的List,并且递归调用自己,直到父节点为“0”,最后,参数的List里的值就是你要的结果了
      

  4.   

    这在oracle中非常简单select ... select ...start with ... connect by prior ...在mysql中里面没有这么便利的内置函数,要么你写存储过程或者函数,要么在自己在java代码端写递归函数,通常是写递归函数的,dba不会随便让你乱改数据库的
      

  5.   

    我以前是这样写的,今天发现my sql 中没有start with ... connect by prior
      

  6.   

    在my sql 中怎么替代上面的SQL啊!!
      

  7.   


        
    //获取表中所有id
    protected List<Long> getIdList(String sql, Object ... params) {
    return xxx;
    }//获取该menu下的所有子节点
    private List<Long> getMenuChildrenIds(long menuId) {
            String sql = "select menu_id from test_tb where p_id = ? ";
            return getIdList(sql, menuId);
    }
        
    public void getAllChildren(long menuId, List<Long> menuIdList) {
            List<Long> childrenIds = getMenuChildrenIds(menuId);
            for (long menu_Id : childrenIds) {
                menuIdList.add(menu_Id);
        //计数
                int count = geliDao.count("select count(1) from test_tb where p_id = ? ", menu_Id, status);
                if (count > 0) {
                    getAllChildren(menu_Id, menuIdList);
                }
            }
    }//
    public List<Menu> getMenuChildren(long menuId) {
            return orm.list(Menu.class, getMenuChildrenIds(menuId    ).toArray());
        }//执行,全找出来menuIds,放到list里面
    List<Long> menuIds = new ArrayList<Long>();
    menuIds.add(menu.getMenuId());
    getAllChildren(menu.getMenuId(), menuIds);
    大概就是这么个意思了,我也胡乱贴上去的,希望对你有帮助
      

  8.   

    mysql不支持这个,楼主可以考虑一下自己写个放在类中
    循环遍历取父ID,拼成string 或者 json
    再或者只有更改表结构,
    如A  id:01
      B  id:0101
      C  id:010101