string str1="1,2,3";
string str2="2,3,4,5";
商品表product
pro_id pro_name
  1 电视
  2 冰箱
  3 水果
  4 床
  5 空调
  11 汽车
  12 飞机
  
相同的只查询一次,得到1,2,3,4,5,而不是1,2,3,2,3,4,5

解决方案 »

  1.   


    declare @str1 varchar(10) set @str1='1,2,3'
    declare @str2 varchar(10) set @str2='2,3,4,5'declare @product table (pro_id int,pro_name varchar(4))
    insert into @product
    select 1,'电视' union all
    select 2,'冰箱' union all
    select 3,'水果' union all
    select 4,'床' union all
    select 5,'空调' union all
    select 11,'汽车' union all
    select 12,'飞机'select * from @product 
    where charindex(','+ltrim(pro_id)+',',','+@str1+',')>0
    or charindex(','+ltrim(pro_id)+',',','+@str2+',')>0
    /*
    pro_id      pro_name
    ----------- --------
    1           电视
    2           冰箱
    3           水果
    4           床
    5           空调
    */
      

  2.   


    declare @str1 varchar(10) set @str1='1,2,3'
    declare @str2 varchar(10) set @str2='2,3,4,5'declare @product table (pro_id int,pro_name varchar(4))
    insert into @product
    select 1,'电视' union all
    select 2,'冰箱' union all
    select 3,'水果' union all
    select 4,'床' union all
    select 5,'空调' union all
    select 11,'汽车' union all
    select 12,'飞机'--稍微简化一下可以这样:
    select * from @product 
    where charindex(','+ltrim(pro_id)+',',','+@str1+','+@str2+',')>0/*
    pro_id      pro_name
    ----------- --------
    1           电视
    2           冰箱
    3           水果
    4           床
    5           空调
    */
      

  3.   

    declare @str1 varchar(10) set @str1='1,2,3'
    declare @str2 varchar(10) set @str2='2,3,4,5'declare @product table (pro_id int,pro_name varchar(4))
    insert into @product
    select 1,'电视' union all
    select 2,'冰箱' union all
    select 3,'水果' union all
    select 4,'床' union all
    select 5,'空调' union all
    select 11,'汽车' union all
    select 12,'飞机'select * from @product 
    where charindex(','+ltrim(pro_id)+',',','+@str1+',')>0
    or charindex(','+ltrim(pro_id)+',',','+@str2+',')>0
    /*
    pro_id      pro_name
    ----------- --------
    1           电视
    2           冰箱
    3           水果
    4           床
    5           空调
    */
    已经解决,网络不好,引用不了,
    申明他已经解决,不是抢劳动果实