表结构:
id     字段1
1     a,b,c,d
2     c,f,g,a
3     e,z,h,b
4     c,f,k,l输入一参数(a,g,h,i) 如何用一条select语句查出参数中的部分内容同字段1部分内容有相同的记录(或说字段1的部分内容是否在参数中),如id为1的a,b,c,d中的a 在参数(a,g,h,i) 则成立; id为4 c,f,k,l中没有数据在参数中 则不成立; 故如果用一条SELECT语句如何输出如下结果:id  字段1
1     a,b,c,d       a在(a,g,h,i)中
2     c,f,g,a       g在(a,g,h,i)中
3     e,z,h,b       h在(a,g,h,i)中

解决方案 »

  1.   

    用一條SQL語句應該無法實現
    可以用函數嗎?
      

  2.   

    兄弟,先讲讲如何实现法呀。最好是最优化的。呵。能够一条SELECT是最理想的:)
      

  3.   

    select * from 表 where colA in (参数) or colB in (参数) or colC in (参数) or colD in (参数)
    如参数是整数不用调整,如参数是字符型的将参数变为('','','','','')的形式
      

  4.   

    create table test(id int, string varchar(1000))
    insert into test values(1,'a,b,c,d')
    insert into test values(2,'c,f,g,a')
    insert into test values(3,'e,z,h,b')
    insert into test values(4,'c,f,k,l')create  table  temp(aaa varchar(20))  
    declare @strsql  as  varchar(8000)  
    select  @strsql=''  
    select  @strsql='insert into temp values('''+replace('a,g,h,i',',',''') insert into temp values (''')+''')' 
    exec (@strsql)  select distinct b.* from temp a,test b
    where charindex(','+a.aaa+',', ','+b.string+',')>0drop table temp
    drop table test/*
    id string
    ---------------
    1 a,b,c,d
    2 c,f,g,a
    3 e,z,h,b
    */
      

  5.   

    --写成存储过程
    create table test(id int, string varchar(1000))
    insert into test values(1,'a,b,c,d')
    insert into test values(2,'c,f,g,a')
    insert into test values(3,'e,z,h,b')
    insert into test values(4,'c,f,k,l')
    gocreate proc proc_test
    @str varchar(100)
    as
    create  table  temp(aaa varchar(20))  
    declare @strsql  as  varchar(8000)  
    select  @strsql=''  
    select  @strsql='insert into temp values('''+replace(''+@str+'',',',''') insert into temp values (''')+''')' 
    exec (@strsql)  
    goexec proc_test 'a,g,h,i'select distinct b.* from temp a,test b
    where charindex(','+a.aaa+',', ','+b.string+',')>0drop table temp
    drop table test
    drop proc proc_test/*
    id string
    ---------------
    1 a,b,c,d
    2 c,f,g,a
    3 e,z,h,b
    */
      

  6.   

    --生成测试数据
    declare @t table(id int,col varchar(100))declare @d varchar(100)
    set @d='a,g,h,i'insert into @t select 1,     'a,b,c,d'
    union all select 2,     'c,f,g,a'
    union all select 3,     'e,z,h,b'
    union all select 4,     'c,f,k,l'--解决方法
    select * from @t where col like '%['+replace(@d,',','')+']%'
      

  7.   

    declare @t table(id int,字段1 varchar(16))
    insert into @t select 
    1,     'a,b,c,d' union all select
    2,     'c,f,g,a' union all select
    3,     'e,z,h,b' union all select
    4,     'c,f,k,l'
    declare @s varchar(32)
    set @s='%[aghi]%'
    select * from @t where patindex(@s,字段1)>0
      

  8.   

    --如果,相隔的都是單字元,倒可以取个巧,否則需要建function或者臨時表之類的.--只適用,相隔單字元
    declare @str varchar(100)
    set @str='a,g,h,i'
    select * from test
    where patindex('%['+replace(string,',','')+']%',@str)>0
      

  9.   

    修正:如果是前台传参数:参数用应用程序处理成 'a','b','c','d'的形式
    'select * from 表 where colA in ('+参数+') or colB in ('+参数+') or colC in ('+参数+') or colD in ('+参数+')'
    如果是sql中传参数
    declare @a varchar(10)
    set @a='a,b,c,d'
    set @a=''''+replace(@a,',',''',''')+''''
    exec('同上')