select * from 表1 iner join 表2 on 表1.bh in(表2.bh)表1.bh内容是一个字符串 如:AA
表2.bh内容是用逗号分开的字符串 如:AA,BB,CC要求如果表1.BH在表2.bh中就进行连接。
这个SQL语句应该怎样写,高手们帮帮

解决方案 »

  1.   

    select m.* , n.* from 表1 m , 表2 n where charindex(',' + m.bh + ',', ',' + n.bh + ',') > 0
      

  2.   

    select * from 表1 
    iner join 表2 on CHARINDEX(','+LTRIM(表1.bh)+',',','+表2.bh+',')>0
      

  3.   

    select * from 表1 a,表2 b
    where charindex(','+a.bh+',',','+b.bh+',')>0
      

  4.   

    select * from 表1 
    join 表2 on CHARINDEX(','+LTRIM(表1.bh)+',',','+表2.bh+',')>0
      

  5.   

    select m.* , n.* from 表1 m , 表2 n where charindex(',' + m.bh + ',', ',' + n.bh + ',') > 0select m.* , n.* from 表1 m , 表2 n where ',' + n.bh + ',' like '%,' + m.bh + ',%'
      

  6.   

    select * from 表1 a,表2 b where charindex(','+a.bh+',',','+b.bh+',')>0
      

  7.   

    用charindex好慢哦。在SQL里运行有时候还超时呢。
      

  8.   

    用charindex好慢,有时候还会运行超时
      

  9.   

    用like更慢.你的需求和设计决定了你的速度.
      

  10.   

    select * from 表1 a , 表2 b where charindex(',' + a.bh + ',', ',' + b.bh + ',') > 0
      

  11.   

    SQL codeselect*from 表1join 表2on 表2.bh like '%表1.bh%'
      

  12.   

    select m.* , n.* from 表1 m , 表2 n where charindex(',' + m.bh + ',', ',' + n.bh + ',') > 0
      

  13.   

    表1:数据结构           
    BH         MC    
    AA1235     MC1
    BB1111X    MC2
    CC2222RX   MC3
    CC3333B    MC4
    PP5555BR   MC5表2:数据结构           
    BH         BHLB      
    AA1235     X,RX,B,BR
    BB1111     X,RX,
    CC2222     B,BR,RX
    CC3333     X,RX,B,BR
    PP5555     B,BR,X怎样才能使表1和表2连接呢?条件是表1.BH=(表2.bh+表2.BHLB的任一个逗号分隔符为组合的字符)
    这样的SQL语句怎样写呢?