问题描述:
我有一个表结构如下:MD1 MD2 MD3   
1 A1 A11   
2 A1 A12
3 A1 A131 A11 A21
2 A11 A221 A12 B21
2 A12 B221 B21 C21
2 B21 C221 D1  D11
2 D1  D121 D11 E11
2 D11 B221 E11 C21
2 E11 C22
然后想用SQL查C21 输出结果 A1 ,D1 或查B22 输入结果A1,D1,请高手指教,在2000的数据库里面如何写语句

解决方案 »

  1.   


    举个例子,如:可以通过  C22->E11->D11->D1    其中除了首尾两个,其他的值会出现在MD2和MD3中,我要的 结果是 通过 C22通过中间的关联查出 D1
      

  2.   

    C22->E11->D11->D1 为什么不是这样的  C22->B21->A12->A1
      

  3.   

    if object_id('TBMD') is not null
    drop table TBMD
    go
    create table TBMD(MD1 int, MD2 varchar(10),MD3 varchar(10))
    go
    insert into TBMD
    select 1, 'A1', 'A11'  
    union all select 2, 'A1', 'A12'
    union all select 3, 'A1', 'A13'union all select 1 , 'A11', 'A21'
    union all select 2 , 'A11', 'A22'union all select 1 , 'A12', 'B21'
    union all select 2 , 'A12', 'B22'union all select 1 , 'B21', 'C21'
    union all select 2 , 'B21', 'C22'union all select 1 , 'D1', 'D11'
    union all select 2 , 'D1', 'D12'union all select 1 , 'D11', 'E11'
    union all select 2 , 'D11', 'B22'union all select 1 , 'E11', 'C21'
    union all select 2 , 'E11', 'C22'
    ;
    with cte as
    (
    select MD1, MD2, MD3 from TBMD where MD3 in ('C21')
    union all select TBMD.MD1, TBMD.MD2, TBMD.MD3 from TBMD inner join cte on TBMD.MD3 = cte.MD2
    )
    select distinct a.MD2 from cte a where a.MD2 not in (select distinct b.MD3 from cte b)/*
    MD2
    ----------
    A1
    D1(2 行受影响)
    */
    --drop table TBMD
      

  4.   

    use test
    go
    if object_id('test.dbo.tb') is not null drop table tb
    -- 创建数据表
    create table tb
    (
    MD1 int,
    MD2 char(4),
    MD3 char(4)
    )
    go
    --插入测试数据
    insert into tb select 1,'A1','A11'
    union all select 2,'A1','A12'
    union all select 3,'A1','A13'
    union all select 1,'A11','A21'
    union all select 2,'A11','A22'
    union all select 1,'A12','B21'
    union all select 2,'A12','B22'
    union all select 1,'B21','C21'
    union all select 2,'B21','C22'
    union all select 1,'D1','D11'
    union all select 2,'D1','D12'
    union all select 1,'D11','E11'
    union all select 2,'D11','B22'
    union all select 1,'E11','C21'
    union all select 2,'E11','C22'
    go
    --代码实现;with t as(
    select * from tb where MD3='C21'
    union all
    select a.* from tb a join t b on a.MD3=b.MD2
    )
    select MD2 from t tt where not exists(select 1 from t where tt.MD2=MD3)/*测试结果MD2
    ---------------------
    D1        
    A1        (2 行受影响)
    */
      

  5.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([MD1] int,[MD2] varchar(3),[MD3] varchar(3))
    insert [tb]
    select 1,'A1','A11' union all
    select 2,'A1','A12' union all
    select 3,'A1','A13' union all
    select 1,'A11','A21' union all
    select 2,'A11','A22' union all
    select 1,'A12','B21' union all
    select 2,'A12','B22' union all
    select 1,'B21','C21' union all
    select 2,'B21','C22' union all
    select 1,'D1','D11' union all
    select 2,'D1','D12' union all
    select 1,'D11','E11' union all
    select 2,'D11','B22' union all
    select 1,'E11','C21'
    go-->测试开始
    -->创建函数
    IF OBJECT_ID('dbo.XiaoAi') IS NOT NULL DROP FUNCTION dbo.XiaoAi
    GO
    CREATE FUNCTION dbo.XiaoAi(@ VARCHAR(20)) 
    RETURNS @t TABLE(id VARCHAR(3), MD2 VARCHAR(3), MD3 VARCHAR(20),Level INT)
    AS 
    BEGIN 
        DECLARE @level INT
        SET @level=1
        INSERT INTO @t SELECT *,@level FROM tb WHERE [MD3]=@
        WHILE(@@ROWCOUNT>0)
            BEGIN 
                SET @level=@level+1
                INSERT INTO @t SELECT t.*,@level FROM tb AS t,@t AS a WHERE a.MD2=t.MD3 AND a.level=@level-1
            END
    RETURN 
    END
    GO
    -->调用函数
    SELECT MD2 from dbo.XiaoAi('C21') a where [Level]=(select max([Level]) from dbo.XiaoAi('C21'))
    -->结果
    /*
    MD2
    ----
    A1
    D1(2 行受影响)*/