select * from part where id in (select left(right(partid,len(partid)-1),len(right(partid,len(partid)-1))-1) from kind where id=1)-----------------请问此语句有什么问题吗?使用此语句没有结果!但语句“select left(right(partid,len(partid)-1),len(right(partid,len(partid)-1))-1) from kind where id=1”的反回值为:" ,1,3,5,7,8,"将问题语句修改为:select * from part where id in (1,3,5,7,8)----------------此语句又能正常得到结果!请高手指点,问题在哪儿?应如何才能得到正确结果呢?

解决方案 »

  1.   

    不一样的
    select * from part where id in ('1,3,5,7,8')
    select * from part where id in (1,3,5,7,8)
      

  2.   

    select * 
    from part 
    where id in 
     (select left(
                 right(partid,len(partid)-1),
                 len(right(partid,len(partid)-1))-1
                 )
      from kind
     )
      

  3.   

    这样应该可以
    declare @s as nvarchar(200)select @s=left(right(partid,len(partid)-1),len(right(partid,len(partid)-1))-1
    from kind where id=1select @s='select * from part where id in (' + @s + ')' print @sexec @s
      

  4.   

    select * 
    from part 
    where id in 
     (select left(
                 right(partid,len(partid)-1),
                 len(right(partid,len(partid)-1))-1
                 )
      from kind 
      where id=1
     )
    里面漏了id=1
      

  5.   

    select * from part where patindex(','+cast(id as varchar(10))+',',(select left(right(partid,len(partid)-1),len(right(partid,len(partid)-1))-1)
    from kind where id=1))>0
      

  6.   

    用charindex来做
    select 
      * 
    from 
      part 
    where 
      charindex(','+cast(id as varchar(10))+',',(select 
      left(right(partid,len(partid)-1),
      len(right(partid,len(partid)-1))-1)
    from 
      kind 
    where 
      id=1))>0
      

  7.   

    ---楼主既然要这样:那就用字符串拼接,然后用 exec,就OK了!
      

  8.   

    实在是不好意思,忘了说明一下使用的数据库了~~
    Access在Access中declare不知道杂用了~~
      

  9.   


    回个贴真难,还得换个号才行!库比较简单:
    表Part:
    id(int)      partname(varchar)     description(memo)    situation(int)
    1             adfsdf                  rwerfsdf             1
    2             sfiwf                  wrw23fsdfdf           1
    3             sdmkls            fsdfsd            1
    ......表Kind:
    id(int)  kindname(varchar)   description(memo)   situation(int)  partid(varchar)
    1         fsdfsd                 dsklwet            1              ,1,3,5,7,8,
    2         sdfsd                kfg34trrre           1                ,3,4,6,8,
    3         rgdf                   kdfdfg             1                ,1,2,5,
    ......