34,590
社区成员
发帖
与我相关
我的任务
分享
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([id] int,[name] nvarchar(25),[type] int,[super] NVARCHAR(100))
Insert #T
select 1,N'admin',1,-1 union all
select 2,N'user2',1,1 union all
select 3,N'user3',1,1 union all
select 4,N'user4',1,3 union all
select 5,N'用户',0,4
Go
--测试数据结束
SELECT [id] ,
[name] ,
[type] ,
CASE WHEN a.type = 0
THEN ( SELECT TOP 1
name
FROM #T
WHERE a.super = #T.id
)
ELSE super
END AS super
FROM #T a
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([id] int,[name] nvarchar(25),[type] int,[super] NVARCHAR(100))
Insert #T
select 1,N'admin',1,-1 union all
select 2,N'user2',1,1 union all
select 3,N'user3',1,1 union all
select 4,N'user4',1,3 union all
select 5,N'用户',0,4
Go
--测试数据结束
SELECT [id] ,
[name] ,
[type] ,
CASE WHEN a.type = 0
THEN ( SELECT TOP 1
name
FROM #T
WHERE a.super = #T.id
)
ELSE super
END AS super
FROM #T a
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([id] int,[name] nvarchar(25),[type] int,[super] NVARCHAR(100))
Insert #T
select 1,N'admin',1,-1 union all
select 2,N'user2',1,1 union all
select 3,N'user3',1,1 union all
select 4,N'user4',1,3 union all
select 5,N'用户',0,4
Go
--测试数据结束
SELECT [id] ,
[name] ,
[type] ,
CASE WHEN a.type = 0
AND a.super = 4 THEN ( SELECT TOP 1
name
FROM #T
WHERE a.super = #T.id
)
ELSE super
END AS super
FROM #T a
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([id] int,[name] nvarchar(25),[type] int,[super] NVARCHAR(100))
Insert #T
select 1,N'admin',1,-1 union all
select 2,N'user2',1,1 union all
select 3,N'user3',1,1 union all
select 4,N'user4',1,3 union all
select 5,N'用户',0,4
Go
--测试数据结束
UPDATE a
SET super = ( SELECT TOP 1
name
FROM #T
WHERE a.super = #T.id
)
FROM #T a
WHERE a.type = 0
AND a.super = 4
SELECT * FROM #T
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([id] int,[name] nvarchar(25),[type] int,[super] NVARCHAR(100))
Insert #T
select 1,N'admin',1,-1 union all
select 2,N'user2',1,1 union all
select 3,N'user3',1,1 union all
select 4,N'user4',1,3 union all
select 5,N'用户',0,4
Go
--测试数据结束
UPDATE #T SET super='user4' WHERE type=0 AND super=4
SELECT * FROM #T
//id,名字,账号类型,账号上一级部门
id name type super
1 admin 1 -1
2 user2 1 1
3 user3 1 1
4 user4 1 3
5 用户 0 4
select id ,username,type,spr from user where type=0
我查询出了全部的用户
然后里面的数据是
5 用户 0 4
其实我想要的数据是
5 用户 0 user4