select a.ID from tab1 a,tab2 b where a.ID=b.id 
b.ID是int类型的,a.ID是varchar类型的就是“b.ID1,b.ID2”这样的
上面的语句应该怎么修改

解决方案 »

  1.   

    select a.ID from tab1 a,tab2 b where a.ID=cast(b.id as varchar(20))  
      

  2.   

    1楼正解。
    建议LZ字段设置时,ID类型设置一样的。
    比如:[ID] [int] IDENTITY(1,1) NOT NULL
      

  3.   

    select a.ID from tab1 a,tab2 b where cast(a.ID as int)=b.id
      

  4.   

    我的意思是b.ID是由2个a.ID组成,中间用,号分开的
      

  5.   

    1楼正解。
    建议LZ字段设置时,ID类型设置一样的。3楼的,如果b表ID有非数字,会出错误。
    CREATE TABLE #a(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [tName] [nvarchar](50) NULL 
     
    )
    CREATE TABLE #b(
    [ID] [nvarchar](20) NOT NULL,
    [tName] [nvarchar](50) NULL 
     
    )
    insert into #a
    select 'a' union all
    select 'b' union all
    select 'c' union all
    select 'd' union all
    select 'e'  insert into #b
    select '1','a' union all
    select '2','b' union all
    select 'n','c' union all
    select 'k','d' union all
    select 'u','e'   select a.* ,b.* from #a a ,#b b where cast(b.id as int)=a.id 
    drop table #a
    drop table #b--(5 行受影响)--(5 行受影响)
    消息 245,级别 16,状态 1,第 25 行
    在将 nvarchar 值 'n' 转换成数据类型 int 时失败。