求一个单表的级联查询

flona1995 2016-11-19 03:59:10
一个department,parent_id就是这个表中的某个department_id ,表示department的上级。可以无限向下扩展。
比如:
一个部门为部门一,id为1,parent为空说明是最大的
一个部门为部门二,parent_id 为1 属于部门一 , id为2
一个部门为部门三,id为2 属于部门二

给定一个department_id 需要找出这个department以及这个department的所有下级,下级的下级,下级的下级的下级……
求个大神


...全文
190 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 2016-11-19
  • 打赏
  • 举报
回复
引用 6 楼 flona1995 的回复:
[quote=引用 5 楼 wmxcn2000 的回复:] MySQL 的库,你怎么不说清楚。。。
我就刚想说你那个是oracle的……[/quote] 这个在 mssql 是可以跑通过的,找 roy_88 给你移到 mysql 版块吧;那边人的比较专业;
flona1995 2016-11-19
  • 打赏
  • 举报
回复
引用 5 楼 wmxcn2000 的回复:
MySQL 的库,你怎么不说清楚。。。
我就刚想说你那个是oracle的……
卖水果的net 2016-11-19
  • 打赏
  • 举报
回复
MySQL 的库,你怎么不说清楚。。。
flona1995 2016-11-19
  • 打赏
  • 举报
回复
引用 3 楼 wmxcn2000 的回复:
[quote=引用 2 楼 flona1995 的回复:]
[quote=引用 1 楼 wmxcn2000 的回复:]

-- 把 100 改成你的 departID
with m as (
select t.id, t.name, t.pid from t where id = 100
union all
select t.id, t.name, t.pid from t,m where t.pid = m.id
)
select * from m






报错了大手子[/quote]

什么错,贴上来;


再贴一些你的测试数据;[/quote]



[SQL]with m as (
select t.department_id, t.department_name, t.parent_id from t_seller_department t where t.department_id = 1
union all
select t.department_id, t.department_name, t.parent_id from t,m where t.parent_id = m.department_id
)
select * from m

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'm as (
select t.department_id, t.department_name, t.parent_id from t_seller' at line 1
卖水果的net 2016-11-19
  • 打赏
  • 举报
回复
引用 2 楼 flona1995 的回复:
[quote=引用 1 楼 wmxcn2000 的回复:]

-- 把 100 改成你的 departID
with m as (
    select t.id, t.name, t.pid from t where id = 100
    union all
    select t.id, t.name, t.pid from t,m where t.pid = m.id
)
select * from m 

报错了大手子[/quote] 什么错,贴上来; 再贴一些你的测试数据;
flona1995 2016-11-19
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:

-- 把 100 改成你的 departID
with m as (
    select t.id, t.name, t.pid from t where id = 100
    union all
    select t.id, t.name, t.pid from t,m where t.pid = m.id
)
select * from m 

报错了大手子
卖水果的net 2016-11-19
  • 打赏
  • 举报
回复

-- 把 100 改成你的 departID
with m as (
    select t.id, t.name, t.pid from t where id = 100
    union all
    select t.id, t.name, t.pid from t,m where t.pid = m.id
)
select * from m 

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧