菜鸟问个数据库问题
时间:2010-09-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>';
?>
代码:
<?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

作者: scyyzgxh 发布时间: 2010-09-04
我感觉不难啊这个
sql="SELECT 姓名 FROM `tablename` WHERE 父亲=$father";
然后循环出姓名
这样你试试
sql="SELECT 姓名 FROM `tablename` WHERE 父亲=$father";
然后循环出姓名
这样你试试
作者: auror 发布时间: 2010-09-04
不会PHP,就用递归!一句SQL解决不了!
不过递归效率差点!
不过递归效率差点!
作者: fonqing 发布时间: 2010-09-05
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28