有如下两张表
table1 :
id  name  value
0   张三   01
1   李四   01,02
2   王五   02,03table2:
id  name value
0   语文  01
1   英语  02
2   数学  03
3   物理  04根据某个条件比如查李四,需要得到输出表
name value
李四  语文,英语sql语句该怎么写??在线等

解决方案 »

  1.   

    ALTER  function test_xx
    (@xx varchar(100))
    returns varchar(100)
    as
    begin
    declare @str varchar(1000)
    select @str=''
    select @str=@str+','+value from table2 where charindex(','+value+',',','+@xx+',')>0
    return stuff(@str,1,1,'')
    endselect name,dbo.test_xx(value) from table1
    参考
    http://topic.csdn.net/u/20070929/15/88945cd5-1b0f-467d-b9af-1df1177b05cf.html
      

  2.   

    select a.name from table1 a left join table2 b on charindex(b.value,a.value)>0