表1
Tnode(nodeid int,propertyId int)
1    1
2    1
3    1
4    2
5    2
6    2
7    3
8    2
9    2
10   1
11   3
12   2
13   2
14   1
15   2
16   2
17   2
18   3表2
Tpath(pathStr varchar(500))
pathStr是由Tnode表中的nodeid组成的字符串
pathStr
1,4,7
1,3,6,9
1,2,5,8
1,4,11,15
1,4,11,16
1,17,12需求:要得出pathStr中propertyId为指定值的nodeId合集
例如 propertyId=2
那么结果应该是: ()内为正确结果,多个值满足条件取第一个
1,4,7         (4)
1,3,6,9       (6)
1,2,5,8       (5)
1,4,11,15     (4)
1,4,11,16     (4)
1,17,12       (17)最后结果(去掉重复项):
4
6
5
17

解决方案 »

  1.   

    好象已经说得很明白了哦..数据之间是什么联系?
    pathStr是由Tnode表中的nodeid组成的字符串 你到底想干什么? 
    需求:要得出pathStr中对应propertyId为指定值的nodeId合集 
    例如 propertyId=2 
    那么结果应该是: ()内为正确结果,多个值满足条件取第一个 
    1,4,7        (4) 
    1,3,6,9      (6) 
    1,2,5,8      (5) 
    1,4,11,15    (4) 
    1,4,11,16    (4) 
    1,17,12      (17) 最后结果(去掉重复项): 



    17
      

  2.   

    CREATE TABLE Tnode(nodeid int,propertyId int) 
    insert tnode select 1,    1 
    union all select 2,    1 
    union all select 3,    1 
    union all select 4,    2 
    union all select 5,    2 
    union all select 6,    2 
    union all select 7,    3 
    union all select 8,    2 
    union all select 9,    2 
    union all select 10,  1 
    union all select 11,  3 
    union all select 12,  2 
    union all select 13,  2 
    union all select 14,  1 
    union all select 15,  2 
    union all select 16,  2 
    union all select 17,  2 
    union all select 18,  3 CREATE table Tpath(pathStr varchar(500)) 
    INSERT INTO Tpath SELECT '1,4,7' 
    union all select '1,3,6,9' 
    union all select '1,2,5,8'
    union all select '1,4,11,15' 
    union all select '1,4,11,16' 
    union all select '1,17,12' DECLARE @pro INT
    SET @pro=2
    DECLARE @a TABLE(nodeid INT,pathstr VARCHAR(100),pos INT)insert @a SELECT   a.nodeid,pathstr,CHARINDEX(','+LTRIM(a.nodeid)+',',','+pathstr+',')
    FROM tnode a,tpath b WHERE CHARINDEX(','+LTRIM(a.nodeid)+',',','+pathstr+',')>0
    and a.propertyId=2 SELECT distinct nodeid FROM @a a WHERE NOT EXISTS(SELECT 1 FROM @a WHERE pathstr=a.pathstr AND pos <a.pos)--result
    /*nodeid      
    ----------- 
    4
    5
    6
    17(所影响的行数为 4 行)*/
      

  3.   

    ---测试数据---
    if object_id('[Tnode]') is not null drop table [Tnode]
    go
    create table [Tnode]([nodeid] int,[propertyId] int)
    insert [Tnode]
    select 1,1 union all
    select 2,1 union all
    select 3,1 union all
    select 4,2 union all
    select 5,2 union all
    select 6,2 union all
    select 7,3 union all
    select 8,2 union all
    select 9,2 union all
    select 10,1 union all
    select 11,3 union all
    select 12,2 union all
    select 13,2 union all
    select 14,1 union all
    select 15,2 union all
    select 16,2 union all
    select 17,2 union all
    select 18,3
    if object_id('[Tpath]') is not null drop table [Tpath]
    go
    create table [Tpath]([pathStr] varchar(9))
    insert [Tpath]
    select '1,4,7' union all
    select '1,3,6,9' union all
    select '1,2,5,8' union all
    select '1,4,11,15' union all
    select '1,4,11,16' union all
    select '1,17,12'
     
    ---查询---
    select distinct
      cast(stuff(
                 right(t1.pathStr,len(t1.pathStr)-t1.ps+1),
                 charindex(',',right(t1.pathStr,len(t1.pathStr)-t1.ps+1)),
                 len(right(t1.pathStr,len(t1.pathStr)-t1.ps+1)),
                 ''
                )
            as int
           )
      as nodeid
    from 
    (
    select 
      min(charindex(ltrim(b.nodeid),a.pathStr)) ps,
      a.pathStr
    from Tpath a
    left join (select * from Tnode where propertyId=2) b 
    on a.pathStr like '%'+ltrim(b.nodeid)+'%'
    group by a.pathStr
    ) t1
    left join (select * from Tnode where propertyId=2) t2 
    on t1.pathStr like '%'+ltrim(t2.nodeid)+'%'
    order by nodeid
    ---结果---
    nodeid      
    ----------- 
    4
    5
    6
    17(所影响的行数为 4 行)
      

  4.   

    给个不用临时表的
    DECLARE @pro INT
    SET @pro=2SELECT distinct SUBSTRING(pathstr,MIN(CHARINDEX(','+LTRIM(a.nodeid)+',',','+pathstr+',')),CHARINDEX(',',pathstr+',',
    MIN(CHARINDEX(','+LTRIM(a.nodeid)+',',','+pathstr+',')))-MIN(CHARINDEX(','+LTRIM(a.nodeid)+',',','+pathstr+','))) nodeid
    FROM tnode a,tpath b WHERE CHARINDEX(','+LTRIM(a.nodeid)+',',','+pathstr+',')>0
    and a.propertyId=2  GROUP BY pathstr