sql="select * from A where a like '%,4,%'" sql="select * from A where charindex(',4,',a)>0"4两边加上 ,号是为了区分4、14、40等含有4的数字

解决方案 »

  1.   

    sql="select * from A where a like '%,4,%'" sql="select * from A where charindex(',4,',a)>0"sql="select * from A where patindex('%,4,%',a)>0"
      

  2.   

    sql="select * from A where a like '%,4,%'
         union all
         select * from A where a =4" 
      

  3.   

    sql="select * from A where a like '%4%'"
      

  4.   

    select * from 表 where ','+a+',' like '%,4,%'--或者:
    select * from 表 where charindex(',4,',','+a+',')>0--或者:
    select * from 表 where patindex('%,4,%',','+a+',')>0
      

  5.   

    用','+a+','的目的就是解决下面记录的查询问题:id   a
    1    4,5
    2    3,4
    3    4
    4    3,4,5
    5    2,14,5如果不用','+a+',',你会发现用楼上几位的方法,检索出来的数据是有问题的.
      

  6.   

    如果是这样的SQL语句呢?A表中有a字段为varChar型,记录关联类别(如果值为3表示关联id为3的类别,如果值为:3,4 表示关联id为3和4的类别),现搜索A表中所有关联B表b字段得记录,如何写该SQL语句??A表中示例数据:A
    id   a
     1    1,2
     2    2,4,5
     3    4
     4    3
     5    1,4B表中示例数据:
    B
    id    b
    1     1,4,7
    2     2,5
    3     4
    所以这样的话就要搜索A表中a字段的规则:如果取B表id=1的A表中的数据:意义:A表中a字段如有有1或4或7都提取。
    如果取B表id=3的A表中的数聚:意义:A表中a字段如果有4则提取。如何编写这样的SQL语句呢?
    sql="select * from 表 where charindex(',4,',','+a+',')>0 or charindex(',1,',','+a+',')>0"??
      

  7.   

    --查询
    select * 
    from A a
    where exists(
    select 1
    from(
    select b from b where id=1
    )aa join(
    select id=a.id+b.id+1
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) a,(
    select id=0 union all select 10
    union all select id=20 union all select 30
    union all select id=40 union all select 50
    union all select id=60 union all select 70
    union all select id=80 union all select 90
    ) b
    )bb on substring(','+b,id,1)=','
    where ','+a.a+',' like '%,'+substring(b,id,charindex(',',b+',',id)-id)+',%'
    )
      

  8.   

    --测试--测试数据
    create table A(id int,a varchar(100))
    insert a  select 1,'1,2'
    union all select 2,'2,4,5'
    union all select 3,'4'
    union all select 4,'3'
    union all select 5,'1,4'create table B(id int,b varchar(100))
    insert B  select 1,'1,4,7'
    union all select 2,'2,5'
    union all select 3,'4'
    go--查询
    select * 
    from A a
    where exists(
    select 1
    from(
    select b from b where id=1
    )aa join(
    select id=a.id+b.id+1
    from(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) a,(
    select id=0 union all select 10
    union all select id=20 union all select 30
    union all select id=40 union all select 50
    union all select id=60 union all select 70
    union all select id=80 union all select 90
    ) b
    )bb on substring(','+b,id,1)=','
    where ','+a.a+',' like '%,'+substring(b,id,charindex(',',b+',',id)-id)+',%'
    )
    go--删除测试
    drop table a,b/*--测试结果
    id          a                    
    ----------- ---------------------
    1           1,2
    2           2,4,5
    3           4
    5           1,4(所影响的行数为 4 行)
    --*/
      

  9.   

    select * from A where a ='4'--'4'
    union all
    select * from A where a like '4,%'--'4'前无后有
    union all
    select * from A where a like '%,4'--'4'后无前有union all
    select * from A where a like '%,4,%'--'4'前后都有
      

  10.   

    对不起我可能没有说明白。我的意识是:A表为留言表a为其一字段,表示该留言的类别关联性(比如a="1"表示关联类别1,a="1,4"表示关联类别1和类别4)B表为会员表b为其一字段,表示该会员所感兴趣的类别(比如b="1"表示对类别1感兴趣,b="1,4"表示对类别1和类别4感兴趣)现在要显示会员所感性取得留言信息。即先搜索B表中b字段信息(如果是1,4代表该会员对1和4类别感兴趣,显示1和4所有留言;如果是1则代表改会员只对类别1感兴趣,知显示类别1的留言,如果为空则表示改会员没有感兴趣的类别不列出任何留言)
    (注:会员的感兴趣类别不一定有多少个也可以一个也没有)
    请问这样的SQL语句怎么写??谢谢~~~