本帖最后由 raclezh 于 2011-07-05 19:30:21 编辑

解决方案 »

  1.   


    --1
    select * from a where charindex(','+gbid+',',','+c_gbid+',')+0   
    --2 最好增加表b的记录,使c_gbid能与gbid一对一,
      

  2.   


    --1
    select * from a where charindex(','+gbid+',',','+c_gbid+',')=0   
    --2 最好增加表b的记录,使c_gbid能与gbid一对一,
      

  3.   

    --1
    select * from a left join b on charindex(','+gbid+',',','+c_gbid+',')=0   
    --2 最好增加表b的记录,使c_gbid能与gbid一对一,
      

  4.   

    你这个方法不可以的,不能得到 表A的gbid 不存在于 表B的c_gbid 里面的记录 。
      

  5.   

    还有更好的方法吗?我现在用的是 先 找到 存在的,然后not in 存在的,这样效率很低。
      

  6.   


    select
      * 
    from
     a 
    where
     not exists(select 1 from a left join b on charindex(','+gbid+',',','+c_gbid+',')=0) 
      

  7.   

    create table A(gbid int, plname varchar(10), plnote varchar(10), pltype int)
    insert a
    select 9 ,'zhao','ceshi', 1 union all
    select 10 ,'li','ceshi', 2 union all
    select 11 ,'sun','ceshi', 2 union all
    select 12 ,'zhang','ceshi', 3 create table b(cjid int, c_gbid varchar(10),barcode int)
    insert b select  1 ,'9,10', 232 union all
     select  2 ,'9,11', 233 union all
     select  3 ,'9,11', 234 union all
     select  4 ,'9,11', 235 union all
     select  5 ,'9,10', 236select  * from a 
    where not exists(select 1 from b 
    where charindex(','+ltrim(a.gbid)+',',','+b.c_gbid+',')<>0) /*
    gbid        plname     plnote     pltype      
    ----------- ---------- ---------- ----------- 
    12          zhang      ceshi      3(所影响的行数为 1 行)
    */
      

  8.   

    这种表设计堪称最差设计.
    且不谈这种设计不能用索引来提高查询速度,而且还要拼凑字符串来进行模糊查询,最基本的问题是:
    你的那个 c_gbid 要设置为多长?你能保证某个码只被扫描有限次吗?这个有限次是多少次?表中对于你这个列的冗余是多大(比如,最大扫描100次,最小扫描1次,你却要让每条记录的这个列长度都在100次的长度)?
    最基本的设置就是
    扫一次,增加一条记录,无论插入更新删除查询,都方便,何乐而不为呢?
    你说是 
    select * from tb where gbid=5
     方便呢,还是
    select * from tb where charindex(',5,',','+c_gbid+',')>0) 
    方便?