当前位置: 代码迷 >> Sql Server >> 请教这个树的sql语句该如何写啊一定给分
  详细解决方案

请教这个树的sql语句该如何写啊一定给分

热度:32   发布时间:2016-04-27 17:51:04.0
请问这个树的sql语句该怎么写啊,一定给分
用户表  
tb_userinfo

userid       username           departmentid
001                 ABC                     001
002                 AAA                     001
003                 VVV                     002
004                 RRR                     002

部门信息表
tb_departmentinfo

departmentid     deparmentname
001                             A部门
002                             B部门

如何查询出树结构的列表?
象这样
userid       username           departmentid       deparmentname
  null           null                     001                         A部门
    001           ABC                       001                         A部门
    002           AAA                       001                         A部门
    null         null                     002                         B部门
    003           VVV                       002                         B部门
    004           RRR                       002                         B部门

快来人拉,在线等待拉




------解决方案--------------------
if object_id( 'pubs..tb_userinfo ') is not null
drop table tb_userinfo
go
create table tb_userinfo(userid varchar(10),username varchar(10),departmentid varchar(10))
insert into tb_userinfo(userid,username,departmentid) values( '001 ', 'ABC ', '001 ')
insert into tb_userinfo(userid,username,departmentid) values( '002 ', 'AAA ', '001 ')
insert into tb_userinfo(userid,username,departmentid) values( '003 ', 'VVV ', '002 ')
insert into tb_userinfo(userid,username,departmentid) values( '004 ', 'RRR ' , '002 ')
go

if object_id( 'pubs..tb_departmentinfo ') is not null
drop table tb_departmentinfo
go
create table tb_departmentinfo(departmentid varchar(10),deparmentname varchar(10))
insert into tb_departmentinfo(departmentid,deparmentname) values( '001 ', 'A部门 ')
  相关解决方案