27,580
社区成员
发帖
与我相关
我的任务
分享
;WITH t(code,pcode)AS
(
select 1,0 UNION
select 2,1 UNION
select 3,2 UNION
select 4,1 UNION
select 5,0
),d(code,data)AS
(
select 1,'a' UNION
select 2,'b' UNION
select 3,'c' UNION
select 4,'d' UNION
select 5,'5'
)
select d.* from d
INNER JOIN t as t1 on t1.code=d.code
LEFT JOIN t as t2 on t2.code=t1.pcode
left join t as t3 on t3.code =t2.pcode
where '1' in (t1.code,t2.code,t3.code)
SELECT
*
FROM Table1 AS a
INNER JOIN TABLE1 AS b ON a.code=b.father_code
INNER JOIN Table1 AS c ON b.code=c.father_code
WHERE c.code='条件'
--查所有三级
SELECT
*
FROM Table1 AS a
INNER JOIN TABLE1 AS b ON a.code=b.father_code
INNER JOIN Table1 AS c ON b.code=c.father_code
WHERE a.father_code=''--第1级没father_code