+ -
当前位置:首页 → 问答吧 → 菜鸟问个数据库问题

菜鸟问个数据库问题

时间:2010-09-04

来源:互联网

a.jpg (21.49 KB)
2010-9-4 19:04

如图所示,表中有姓名、父亲两列值,姓名是唯一的。。
现在想查询某个姓名它所在的父系。。
比如我要查询姓名为10所在的父系,那么结果应该返回6、3、1
若查询姓名13,则返回10、6、3、1
这样的查询语句能写吗?关系是一级级的。。。
请教高人。。。

作者: scyyzgxh   发布时间: 2010-09-04

这其实就是个无限分类!
如果后台端你是用的 PHP,那么恭喜你,我这里正好有一个你需要的类!
复制内容到剪贴板
代码:
<?php
/**
* @author        YangHuan
* @datetime   
* @version        1.0.0
*/
/**
* Short descrīption.
*
* Detail descrīption
* @author      
* @version      1.0
* @copyright   
* @access       public
*/
class Tree{
    /**
     * Descrīption
     * @var      
     * @since     1.0
     * @access    private
     */
    var $data    = array();
   
    /**
     * Descrīption
     * @var      
     * @since     1.0
     * @access    private
     */
    var $child    = array(-1=>array());
   
    /**
     * Descrīption
     * @var      
     * @since     1.0
     * @access    private
     */
    var $layer    = array(-1=>-1);
   
    /**
     * Descrīption
     * @var      
     * @since     1.0
     * @access    private
     */
    var $parent    = array();
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function Tree (){
        $this->setNode(0, -1, '根分类');
    } // end func
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function setNode ($id, $parent, $value){
        $parent = $parent?$parent:0;
        $this->data[$id]            = $value;
        $this->child[$id]            = array();
        $this->child[$parent][]        = $id;
        $this->parent[$id]            = $parent;
        $this->layer[$id] = !isset($this->layer[$parent]) ? 0 : $this->layer[$parent] + 1;
    } // end func
   
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getList (&$tree, $root= 0){
        foreach ($this->child[$root] as $key=>$id){
            $tree[] = $id;
            if ($this->child[$id]) $this->getList($tree, $id);
        }
    } // end func
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getValue ($id){
        return $this->data[$id];
    } // end func
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getLayer ($id, $space = false){
        return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
    } // end func
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getParent ($id){
        return $this->parent[$id];
    } // end func
   
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getParents ($id){
        while ($this->parent[$id] != -1){
            $id = $parent[$this->layer[$id]] = $this->parent[$id];
        }
        ksort($parent);
        reset($parent);
        return $parent;
    } // end func
   
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getChild ($id){
        return $this->child[$id];
    } // end func
   
    /**
     * Short descrīption.
     *
     * Detail descrīption
     * @param      none
     * @global     none
     * @since      1.0
     * @access     private
     * @return     void
     * @update     date time
    */
    function getChilds ($id = 0){
        $child = array($id);
        $this->getList($child, $id);
        return $child;
    } // end func
    function setUpTree($array){
        if( is_array($array) ) {
            foreach($array as $value){
                $this->setNode($value['catid'], $value['pcatid'], $value['catname']);
            }
        }
    }
} // end class
$Tree = new Tree();
//setNode(目录ID,上级ID,目录名字);
$Tree->setNode(1, 0, '目录1');
$Tree->setNode(2, 0, '目录2');
$Tree->setNode(3, 0, '目录3');
$Tree->setNode(4, 3, '目录3.1');
$Tree->setNode(5, 3, '目录3.2');
$Tree->setNode(6, 3, '目录3.3');
$Tree->setNode(7, 2, '目录2.1');
$Tree->setNode(8, 2, '目录2.2');
$Tree->setNode(9, 2, '目录2.3');
$Tree->setNode(10, 6, '目录3.3.1');
$Tree->setNode(11, 6, '目录3.3.2');
$Tree->setNode(12, 6, '目录3.3.3');
$category = $Tree->getParents(12);
echo '<pre>';
print_r($category);
echo '</pre>';
?>

作者: fonqing   发布时间: 2010-09-04

恭喜过早了,我不会php的。。。有谁能写出sql语句来吗?

作者: scyyzgxh   发布时间: 2010-09-04

我感觉不难啊这个
sql="SELECT 姓名  FROM `tablename` WHERE 父亲=$father";
然后循环出姓名
这样你试试

作者: auror   发布时间: 2010-09-04

不会PHP,就用递归!一句SQL解决不了!
不过递归效率差点!

作者: fonqing   发布时间: 2010-09-05