表自身的内链接问题

FrameSniper 2008-10-09 01:39:32
我现在有表a,其中三个字段index,name,superiorindex,其中superiorindex引用字段index,表形成自引用

我现在想写一个内链查询,把name和superiorindex对应的那么现实出来,怎么写
...全文
79 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfq85825 2008-10-09
  • 打赏
  • 举报
回复
不对,是
select a.name ,b.superioridex from tab a,tab b where a.index1=b.superioridex
name superioridex
王 1
李 3
张 5

yfq85825 2008-10-09
  • 打赏
  • 举报
回复

create table tab(index1 int,name varchar(16),superioridex int)
insert into tab select 1,'王',3 union all
select 3,'李',5 union all
select 5,'张',1

select b.name ,b.superioridex from tab a,tab b where a.index1=b.superioridex
index1 name superioridex
1 王 3
3 李 5
5 张 1

---lz是不要这结果啊-------
name superioridex
王 3
李 5
张 1
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
谢谢 pt1314917
fcuandy 2008-10-09
  • 打赏
  • 举报
回复


DECLARE @t TABLE([index] INT,name VARCHAR,superiorindex INT)
INSERT @t SELECT 1,'a',NULL
UNION ALL SELECT 2,'b',1
UNION ALL SELECT 3,'c',2
SELECT a.[index],a.name,b.name FROM @t a
LEFT JOIN @t b
ON a.superiorindex = b.[index]
/*
1 a NULL
2 b a
3 c b
*/
yfq85825 2008-10-09
  • 打赏
  • 举报
回复
select a.name ,a.superioridex from tab a,tab b where a.index1=b.superioridex
水族杰纶 2008-10-09
  • 打赏
  • 举报
回复
拿點數據說及
想要的結果
pt1314917 2008-10-09
  • 打赏
  • 举报
回复

--这样。
select df_Name as 名称,
(select df_Name from udfdt_Department where df_Index=a.df_SuperiorDeptIndex) as 上级部门
from udfdt_Department a
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
但无论b.df_Name哪里加不加distinct都说什么子查询返回记录数量大于1个
FrameSniper 2008-10-09
  • 打赏
  • 举报
回复
我是这么些的

create procedure udsp_SelectDepartmentTableContent
as
declare @SelectStatement nvarchar(512)
set @SelectStatement='select df_Name as 名称,(select b.df_Name from udfdt_Department b inner join udfdt_Department a on b.df_Index=a.df_SuperiorDeptIndex) as 上级部门 from udfdt_Department'
execute(@SelectStatement)

go
twgxzjk 2008-10-09
  • 打赏
  • 举报
回复
是不是这样:
select * from a aa
where name=(select superiorindex from a where name=aa.name)
-狙击手- 2008-10-09
  • 打赏
  • 举报
回复
..
liangCK 2008-10-09
  • 打赏
  • 举报
回复
SELECT *
FROM tba AS a
JOIN tba AS b
ON a.superiorindex=b.index


这样??

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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