当前位置:首页 » 编程语言 » sql下级查询
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql下级查询

发布时间: 2022-02-25 22:40:57

sql如何取某个部门的所有子部门

假设表名为 tb, id=001
select * from tb where parentid=(select parentid from tb where id=001)
上面的语句只能找到下级部门,三级就找不到了。建议部门编码使用层级式的,这样就很容易了

Ⅱ sql server 中如何实现查找下级分类以及下下级

在文件 includes/lib_goods.php 最后加上

//***调用商品分类指定分类下级分类
functionget_parent_id_tree($parent_id)
{
$three_c_arr=array();
$sql='SELECTcount(*)FROM'.$GLOBALS['ecs']->table('category')."WHEREparent_id='$parent_id'ANDis_show=1";
if($GLOBALS['db']->getOne($sql))
{
$child_sql='SELECTcat_id,cat_name,parent_id,is_show'.
'FROM'.$GLOBALS['ecs']->table('category').
"WHEREparent_id='$parent_id'ANDis_show=1ORDERBYsort_orderASC,cat_idASC";
$res=$GLOBALS['db']->getAll($child_sql);
foreach($resAS$row)
{
if($row['is_show'])
$three_c_arr[$row['cat_id']]['id']=$row['cat_id'];
$three_c_arr[$row['cat_id']]['name']=$row['cat_name'];
$three_c_arr[$row['cat_id']]['url']=build_uri('category',array('cid'=>$row['cat_id']),$row['cat_name']);
}
}
return$three_c_arr;
}

声明后用$smarty调用,就是在index.php中加上下面一句:
$smarty->assign('get_parent_id16_tree',get_parent_id_tree(16));//调用父级分类6的下级分类
最后就可以在index.dwt模板文件里开始调用了
<!--{foreachfrom=$get_parent_id16_treeitem=list}-->
<ahref="http://chenlihong89791781.blog.163.com/{$list.url}"target="_blank">{$list.name|truncate:15:true}</a>
|<!--{/foreach}--></div>

Ⅲ SQL语句怎么获取下级所有部门

一级部门的所有下级部门就用 like "01-%" 即可,方便实惠。
你现在的编号么,也可以用这个方式转换一下,例如加个字段,再把编号都这样连起来放这个编号里专门做查询用。

Ⅳ SQL语句查询出父节点下的所有子节点


createtable##tmp_users(idint,usernamenvarchar(255),parentidint)
declare@IDint
select@ID=idfromt_Userstwhereexists
(select*fromt_Userst2wheret2.id=t.parentidandt2.username='user1')
execAddSons@ID
select*from##tmp_users
droptable##tmp_users


--存储
createprocereAddSons@idint
as
ifexists(select*fromt_Userswhereparentid=@id)
begin
declare@tmp_IDint
declarecurcursorfor
selectidfromt_Userswhereparentid=@id
opencur
fetchnextfromcurinto@tmp_ID
while@@FETCH_STATUS=0
begin
insertinto##tmp_users
select*fromt_Userstwhereid=@tmp_ID
ifexists(select*fromt_Userswhereparentid=@tmp_ID)
begin
execAddSons@tmp_ID
end
fetchnextfromcurinto@tmp_ID
end
closecur
DEALLOCATEcur
end
--递归调用,不知道是否想要这样

Ⅳ sql 查询所有最下级元素

我的空间中有一篇关于with 实现sql递归查询的

http://hi..com/micro0369/item/5a89cc277733081e0975086c

Ⅵ sql语句:如何通过id查找所有的子节点

使用SQL递归查询

Declare@IdInt
Set@Id=0;---在此修改父节点
WithRootNodeCTE(D_ID,D_FatherID,D_Name,lv)
As
(
SelectD_ID,D_FatherID,D_Name,0aslvFrom[LFBMP.LDS].[dbo].[LDS.Dictionary]WhereD_FatherIDIn(@Id)
UnionAll
Select[LFBMP.LDS].[dbo].[LDS.Dictionary].D_ID,[LFBMP.LDS].[dbo].[LDS.Dictionary].D_FatherID,[LFBMP.LDS].[dbo].[LDS.Dictionary].D_Name,lv+1FromRootNodeCTE
InnerJoin[LFBMP.LDS].[dbo].[LDS.Dictionary]
OnRootNodeCTE.D_ID=[LFBMP.LDS].[dbo].[LDS.Dictionary].D_FatherID
)
Select*FromRootNodeCTE

;WithTB([Cd_ID],[ConstituteID],[Cd_PID],[Cd_CName],lv)
as(
Select[Cd_ID],[ConstituteID],[Cd_PID],[Cd_CName],0aslvFROM[LFBMP.Center].[dbo].[ConstituteDetail]Where[Cd_PID]=0And[ConstituteID]=4
unionall
SelectA.[Cd_ID],A.[ConstituteID],A.[Cd_PID],A.[Cd_CName],lv+1FROMTBinnerjoin[LFBMP.Center].[dbo].[ConstituteDetail]asA
onTB.[Cd_ID]=A.Cd_PID
)
Select*FromTB

Ⅶ sql查询: 有上下级关系的表如何连接

select c.班级编号,c.学生编号,d.学生姓名,c.成绩
from c, (select * from a,b where a.主键 = b.班级主键) d
where c.班级编号 = d.班级编号 and c.学生编号 = d.学生编号
order by c.班级编号,c.学生编号

Ⅷ 关于sql的上下级查询统计(如图)

selectcode_name,amountfrom
(selectleft(code,2)ascode,ISNULL(SUM(amount),0)asamount
fromagroupbyleft(code,2))astmp
innerjoinbontmp.code=b.code

Ⅸ 高难!小白求救,sql 每层有一到三个下级如何查询子树

类似这种语法,你可以参考一下:

WITH leaderCTE as(
SELECT id, name,manager
FROM [lulinghao].[dbo].[YGB]
WHERE name = @name

UNION ALL

SELECT ygb.id, ygb.name,ygb.manager
FROM [lulinghao].[dbo].[YGB] as ygb
inner join leaderCTE lce on lce.manager = ygb.id
)
SELECT case when [id]=100 then 1 when [id]=101 OR [id]=102 then 2 else 3 end as leaderlevel,
name as leadername
FROM leaderCTE

Ⅹ SQL 多级查询(级数不定)

select * from item where itemID=3 union all
select * from item where FDetail=0 and parentID <(select parentID from item where itemID=3)
and substring(Number,1,2) =(select substring(Number,1,2) from item where itemID=3)
and substring(Number,4,2) =(select substring(Number,4,2) from item where itemID=3);

说下思路,先把自己本身一条找出来,然后找他的上级,看你的数据知道parentID 一定小于本身的parentID ,并且是目录的话FDetail=0,如果是其上级目录,他们前边的01.01什么的都是一样的,但是现在有个弊端,就是查询前,要确定这个itemID=3的是属于第几级实体,然后才能采用后边用多少个substring,另一个表itemID=3的条件没用,其实就是一个嵌套,你自己写里边吧