2

我想从下表中选择与 user_id 53(父母和孩子)相关的所有项目。应该是:1、2、4、8、9。

my_table
--------------------------------------------
id    parent_id   user_id   sequence   depth
--------------------------------------------
1     null        50        1          1
2     1           52        1.2        2
3     1           52        1.3        2
4     2           53        1.2.4      3
5     2           52        1.2.5      3
6     3           52        1.3.6      3
7     3           51        1.3.7      3
8     4           51        1.2.4.8    4
9     4           51        1.2.4.9    4

使用 CTE,我可以选择所有孩子或父母,但我不能只用一个查询来选择孩子和父母。下面是我用来选择孩子的 cte。

项目和孩子

with cte as (
    select t.id, t.parent_id, t.user_id 
    from my_table t 
    where t.user_id=53

    union all

    select t.id, t.parent_id, t.user_id 
    from my_table t
    inner join cte c on (c.parent_id=t.id)
)
select t.* from cte t;

项目和父母

with cte as (
    select t.id, t.parent_id, t.user_id 
    from my_table t 
    where t.user_id=53

    union all

    select t.id, t.parent_id, t.user_id 
    from my_table t
    inner join cte c on (c.id=t.parent_id)
)
select t.* from cte t;

谢谢。

4

4 回答 4

4

拥有序列非常方便。父母有一个与您正在寻找的初始子集相匹配的序列。对孩子来说也是如此,但反过来。

以下内容接近您想要的:

select mt.*
from (select sequence from my_table where USER_ID = 53) theone join
     my_table mt
     on mt.sequence like theone.sequence+ '%' or
        theone.sequence like mt.sequence + '%'

但是,例如,您必须小心10匹配1。因此,让我们在适当的地方添加一个额外的时间段:

select mt.*
from (select sequence from my_table where USER_ID = 53) theone join
     my_table mt
     on mt.sequence like theone.sequence+ '.%' or
        theone.sequence like mt.sequence + '.%'
于 2013-03-04T14:17:43.820 回答
0

这里的问题是,你想要一个递归 SELECT的. 不为此类查询创建 DBMS。但这当然是可能的。

SELECT t1.* FROM Table1 t1 WHERE user_id = 53

UNION ALL

SELECT t2.* FROM Table1 t1, Table1 t2
WHERE
  (t1.id = t2.parent_id OR t1.parent_id = t2.id) AND t1.user_id = 53

此查询将为您提供每个 1 级亲属。

对于递归 SELECT,请看这里:SQL 中的递归选择


此处查询的解决方案:http ://sqlfiddle.com/#!6/e1542/10/0

with cte as (
    select t.id, t.parent_id, t.user_id 
    from Table1 t 
    where t.user_id=53
),

cte1 as (
    select t.id, t.parent_id, t.user_id 
    from cte t

    union all

    select t.id, t.parent_id, t.user_id 
    from Table1 t
    inner join cte1 c on (c.parent_id=t.id)
),

cte2 as (
    select t.id, t.parent_id, t.user_id 
    from cte t

    union all

    select t.id, t.parent_id, t.user_id 
    from Table1 t
    inner join cte2 c on (c.id=t.parent_id)
)

select t.* from cte1 t

UNION

select t.* from cte2 t
于 2013-03-04T12:26:54.840 回答
0
declare @Rownumber int;
CREATE TABLE #GetSeqUser (
    RowNumber int,
    UserId int,
)
INSERT INTO #GetSeqUser Select ROW_NUMBER() OVER(ORDER BY EmpId ASC) AS ROWNUMBER, EmpId FROM  dbo.TreeSystem
set @Rownumber = (select ROWNUMBER from #GetSeqUser where UserId = 3 )
set @Rownumber = @Rownumber + 1
select UserId from #GetSeqUser where ROWNUMBER = @Rownumber
于 2022-01-06T07:51:34.633 回答
0
declare @Rownumber int;
CREATE TABLE #GetSeqUser (
    RowNumber int,
    UserId int,
)
INSERT INTO #GetSeqUser Select ROW_NUMBER() OVER(ORDER BY EmpId ASC) AS ROWNUMBER, EmpId FROM  dbo.TreeSystem
set @Rownumber = (select ROWNUMBER from #GetSeqUser where UserId = 3 )
set @Rownumber = @Rownumber + 1
select UserId from #GetSeqUser where ROWNUMBER = @Rownumber
于 2022-01-06T07:47:06.703 回答