当前位置:首页 » 数据仓库 » 递归数据库好吗
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

递归数据库好吗

发布时间: 2022-04-20 21:20:48

⑴ python中序列能被看作是递归的数据库么,原话见补充

应该 不是这么理解哦。 我的理解是这样的。

一个序列, 可以视为一组递归的数据收集行为。

换句话说,应该是可以用递归,产生一个系列的数据。

⑵ 数据库语句的递归查询求助

应该是这样:

with qry as (select user_id,parent_id from tab where user_id = 32
union all
select tab.user_id,tab.parent_id from tab,qry
where tab.parent_id = qry.id)
select * from qry ;

我用mysql5.0.22,不支持上述语法,oracle就可以(sqlserver应该也可以):
create table tab1(user_id int, parent_id int);
insert into tab1 values(1,null);
insert into tab1 values(32,1);
insert into tab1 values(101,32);
insert into tab1 values(102,32);
insert into tab1 values(201,101);
insert into tab1 values(202,101);
insert into tab1 values(203,102);
insert into tab1 values(204,102);
select * from tab1;
with qry(user_id,parent_id) as (select user_id,parent_id from tab1 where user_id = 32
union all
select tab1.user_id,tab1.parent_id from tab1,qry
where tab1.parent_id = qry.user_id
)
select * from qry;

所以,mysql没有办法了,只有写函数,用循环来实现了。

⑶ 怎么用递归 绑定数据库 显示在treeview中

这是以前写的,可以参考一下!

treeView里面实现数据绑定

DataSetds;

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!IsPostBack)

{

//以下是绑定数据

SqlConnectioncon=new

SqlConnection("server=.;database=Test;uid=sa;pwd=sa");

SqlDataAdapteradapter=newSqlDataAdapter("select*fromTreeViewTest",con);

ds=newDataSet();

adapter.Fill(ds,"TreeViewTest");

//初始化TreeView:TreeView1.Nodes:表示TreeView1的节点集合,"0"表示的是最顶上节点没有父节点

InitTree(TreeView1.Nodes,"0");

}

}

///<summary>

///初始化TreeView

///</summary>

///<paramname="Nds">TreeNodeCollection:节点的集合</param>

///<paramname="parentId">parentId:某一节点的父节点ID</param>

privatevoidInitTree(TreeNodeCollectionNds,stringparentId)

{

DataViewdv=newDataView();

TreeNodetmpNd;

//stringintId;

dv.Table=ds.Tables["TreeViewTest"];

//筛选出ParentId【父节点ID】为传入值【parentId】的所有的数据行为"0"时表时选出所有的顶节点数据行

dv.RowFilter="ParentId='"+parentId+"'";

//将筛选出的所有的数据行添加到节点

foreach(DataRowViewdrvindv)

{

tmpNd=newTreeNode();

tmpNd.Value=drv["NodeId"].ToString();

tmpNd.Text=drv["NodeName"].ToString();

Nds.Add(tmpNd);

//判断是不是存在drv["NodeId"]的ChildNodes,存在的话添加,不存在就继续遍历

DataViewdvTemp=newDataView();

dvTemp.Table=ds.Tables["TreeViewTest"];

dvTemp.RowFilter="ParentId='"+drv["NodeId"].ToString()+"'";

if(dvTemp.Count!=0)

{

InitTree(tmpNd.ChildNodes,tmpNd.Value);

}

}

}

⑷ c#语言递归显示数据数据库语句查询

呵呵,算是有缘,这是一道公司面试题目吧,我以前碰到过一模一样的题目,今天看到你提问就把那以前我弄不出来的题目翻出来写了一次,我想他给你的数据库文件和我的一模一样,因为数据都是一样的,下面是代码,新鲜出炉的代码,加油看,如果看不懂的话可以问我!

Default.aspx.cs文件中代码

usingSystem;

usingSystem.Data;

usingSystem.Data.SqlClient;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Collections.Generic;

publicpartialclass_Default:System.Web.UI.Page

{

protectedDictionary<string,string>classdic=newDictionary<string,string>();//名称字典

protectedList<string>lists=newList<string>();//编号泛型

protectedDictionary<string,int>orders=newDictionary<string,int>();//排序号字典

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!IsPostBack)

{

GetClassList("0",0);

stringisorder=Request.QueryString["isorder"];

stringid=Request.QueryString["id"];

switch(isorder)

{

case"del":

{

if(Del(id)>0)

{

Response.Write("<scripttype='text/javascript'>alert('删除成功');location.href('default.aspx');</script>");

}

else

{

Response.Write("<scripttype='text/javascript'>alert('删除失败');location.href('default.aspx');</script>");

}

}

break;

case"up":

{

Updates(id,0);

Response.Write("<scripttype='text/javascript'>location.href('default.aspx');</script>");

}

break;

case"down":

{

Updates(id,1);

Response.Write("<scripttype='text/javascript'>location.href('default.aspx');</script>");

}

break;

}

}

}

///<summary>

///递归读出数据库方法

///</summary>

///<paramname="ParentID">父ID</param>

///<paramname="Layer">级数,这里只有一级,所以最后也只加了2个空格</param>

protectedvoidGetClassList(stringParentID,intLayer)

{

IDataReaderdr=ClassListDAL(ParentID);

Dictionary<string,string>classdic1=newDictionary<string,string>();

List<string>lists1=newList<string>();

while(dr.Read())

{

classdic1.Add(dr["categoryID"].ToString().Trim(),dr["categoryName"].ToString());

lists1.Add(dr["categoryID"].ToString().Trim());

orders.Add(dr["categoryID"].ToString().Trim(),int.Parse(dr["orderIndex"].ToString()));

}

dr.Close();

for(inti=0;i<classdic1.Count;i++)

{

stringstxt="";

if(Layer>0)

stxt="&nbsp;&nbsp;";

for(intj=1;j<Layer;j++)

{

stxt+="&nbsp;&nbsp;&nbsp;&nbsp;";

}

classdic.Add(lists1[i],stxt+classdic1[lists1[i]]);

lists.Add(lists1[i]);

GetClassList(lists1[i],(Layer+1));

}

}

///<summary>

///读取数据库信息

///</summary>

///<paramname="ParentID">父ID</param>

///<returns></returns>

(stringParentID)

{

SqlConnectionconn=newSqlConnection("server=.\SQLEXPRESS;uid=sa;pwd=123456;database=interView");//数据库连接为我本地连接

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandType=CommandType.Text;

cmd.CommandText="select*from[shop_category]whereparentID=@parentIDorderbyorderIndexasc";

SqlParameterparam=newSqlParameter("@parentID",ParentID);

cmd.Parameters.Add(param);

conn.Open();

returncmd.ExecuteReader(CommandBehavior.CloseConnection);

}

///<summary>

///删除方法,没测试过,不过应该没问题

///</summary>

///<paramname="ids">信息编号</param>

///<returns></returns>

protectedintDel(stringids)

{

SqlConnectionconn=newSqlConnection("server=.\SQLEXPRESS;uid=sa;pwd=123456;database=interView");//数据库连接为我本地连接

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandType=CommandType.Text;

cmd.CommandText="deletefrom[shop_category]wherecategoryID=@categoryID";

SqlParameterparam=newSqlParameter("@categoryID",ids);

cmd.Parameters.Add(param);

conn.Open();

inti=cmd.ExecuteNonQuery();

conn.Close();

returni;

}

///<summary>

///升降方法

///</summary>

///<paramname="ids">信息ID</param>

///<paramname="i">升降编号0为升1为降</param>

protectedvoidUpdates(stringids,inti)

{

SqlConnectionconn=newSqlConnection("server=.\SQLEXPRESS;uid=sa;pwd=123456;database=interView");//数据库连接为我本地连接

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandType=CommandType.Text;

SqlParameterparam=newSqlParameter("@id",ids);

cmd.Parameters.Add(param);

if(i==0)

{

cmd.CommandText="selecttop1*from[shop_category]whereparentID=(selectparentIDfrom[shop_category]wherecategoryID=@id)andorderIndex<(selectorderIndexfrom[shop_category]wherecategoryID=@id)orderbyorderIndexdesc";

}

else

{

cmd.CommandText="selecttop1*from[shop_category]whereparentID=(selectparentIDfrom[shop_category]wherecategoryID=@id)andorderIndex>(selectorderIndexfrom[shop_category]wherecategoryID=@id)orderbyorderIndexasc";

}

conn.Open();

SqlDataReaderread=cmd.ExecuteReader();

if(read.Read())

{

stringid=read["categoryID"].ToString();

intorderid=int.Parse(read["orderIndex"].ToString());

read.Close();

SqlParameterpas=newSqlParameter("@orderid",orderid);

cmd.Parameters.Add(pas);

cmd.CommandText="Update[shop_category]setorderIndex=@orderidwherecategoryID=@id";

if(cmd.ExecuteNonQuery()>0)

{

cmd.Parameters[0].Value=id;

cmd.Parameters[1].Value=int.Parse(Request.QueryString["orid"]);

cmd.CommandText="Update[shop_category]setorderIndex=@orderidwherecategoryID=@id";

cmd.ExecuteNonQuery();

conn.Close();

}

}

}

}

Default.aspx文件代码

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>无标题页</title>

</head>

<body>

<formid="form1"runat="server">

<tablecellpadding="3"cellspacing="3"border="1">

<trstyle="background-image:url(images/tablethbg.gif)">

<tdstyle="width:30%">类别名称</td>

<tdstyle="width:20%">排序</td>

<tdstyle="width:20%">修改</td>

<tdstyle="width:30%">删除</td>

</tr>

<%for(inti=0;i<lists.Count;i++)

{%>

<tr>

<td><%=classdic[lists[i]]%></td>

<td><ahref="Default.aspx?isorder=up&id=<%=lists[i]%>&orid=<%=orders[lists[i]]%>"><imgsrc="images/up.gif"alt=""/></a>&nbsp;<ahref="Default.aspx?isorder=down&id=<%=lists[i]%>&orid=<%=orders[lists[i]]%>"><imgsrc="images/down.gif"alt=""/></a></td>

<td><ahref="#">修改</a></td>

<td><ahref="javascript:if(window.confirm('你确定要删除吗?')){location.href('Default.aspx?isorder=del&id=<%=lists[i]%>');}">删除</a></td>

</tr>

<%}%>

</table>

</form>

</body>

</html>

<scripttype="text/javascript">

</script>

测试结果如图,我没写样式的,希望你能通过面试,不过你还需要加强知识啊~~~

⑸ java 递归数据库生成 树形结构问题

1、准备表结构及对应的表数据
a、表结构:
create table TB_TREE
(
CID NUMBER not null,
CNAME VARCHAR2(50),
PID NUMBER //父节点
)

b、表数据:

insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0);
insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1);
insert into tb_tree (CID, CNAME, PID) values (3, '广东省', 1);
insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);
insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 3);
insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3);
insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 5);
insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 5);
insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 6);
insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 6);
insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 2);
insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 4);

2、TreeNode对象,对应tb_tree

public class TreeNode implements Serializable {
private Integer cid;
private String cname;
private Integer pid;
private List nodes = new ArrayList();

public TreeNode() {
}

//getter、setter省略
}

3、测试数据

public class TreeNodeTest {
@Test
public void loadTree() throws Exception{
System.out.println(JsonUtils.javaToJson(recursiveTree(1)));
}

/**
* 递归算法解析成树形结构
*
* @param cid
* @return
* @author jiqinlin
*/
public TreeNode recursiveTree(int cid) {
//根据cid获取节点对象(SELECT * FROM tb_tree t WHERE t.cid=?)
TreeNode node = personService.getreeNode(cid);
//查询cid下的所有子节点(SELECT * FROM tb_tree t WHERE t.pid=?)
List childTreeNodes = personService.queryTreeNode(cid);
//遍历子节点
for(TreeNode child : childTreeNodes){
TreeNode n = recursiveTree(child.getCid()); //递归
node.getNodes().add(n);
}

return node;
}
}

输出的json格式如下:

{
"cid": 1,
"nodes": [
{
"cid": 2,
"nodes": [
{
"cid": 11,
"nodes": [

],
"cname": "密云县",
"pid": 2
}
],
"cname": "北京市",
"pid": 1
},
{
"cid": 3,
"nodes": [
{
"cid": 5,
"nodes": [
{
"cid": 7,
"nodes": [

],
"cname": "海珠区",
"pid": 5
},
{
"cid": 8,
"nodes": [

],
"cname": "天河区",
"pid": 5
}
],
"cname": "广州市",
"pid": 3
},
{
"cid": 6,
"nodes": [
{
"cid": 9,
"nodes": [

],
"cname": "福田区",
"pid": 6
},
{
"cid": 10,
"nodes": [

],
"cname": "南山区",
"pid": 6
}
],
"cname": "深圳市",
"pid": 3
}
],
"cname": "广东省",
"pid": 1
},
{
"cid": 4,
"nodes": [
{
"cid": 12,
"nodes": [

],
"cname": "浦东",
"pid": 4
}
],
"cname": "上海市",
"pid": 1
}
],
"cname": "中国",
"pid": 0
}

⑹ 高级数据库设计中,什么是递归关系递归关系有几种什么是交叠规则和分离规则

递归关系就是对自身的引用,多媒体数据库中就是记录与记录之间的直接或间接的调用。递归关系有两种 一对多 多对多 至于分离和交叠规则 就是 ,父类实例与子类实体之间的关系。若父类的实例属于多个字类实体则是交叠规则,若父类实例至多属于一个子类实体则是分离规则。不用谢!^_^

⑺ [PHP][THNIKPHP]如何递归查询数据库

function get_category($id){
$str=array();
//$sql = "select * from biao where id=$id";查询节点,自己写吧
$result = array('id'=>,'parent_id'=>);//查询结果一个数组格式
if($result){
$str = get_category($result['parent_id']);
$str[]=$result;
}
return $str;
}
}
调用get_category()就行了,$str第一个元素是节点本身,去掉就行了。

⑻ 递归读取数据库的存储过程

首先设计数据就得注意,比如说:表有3列 id,name,parentId.有这样几条数据1 'a' 02 'b' 03 'c 1只要存储过程写成根据parentId查询就行。先把parentId=0的数据取出来,然后再根据1取去3

⑼ 数据库递归耗的资源比服务器递归还要多吗

那应该是查出后再程序里递归好一点,估计是树形结构的数据吧,一般都是查出全部,然后程序里递归,不然你不停的执行命令肯定浪费性能。