table1id  color
1   blue
2   red
3   whitetable2
id   size
1     20
2     21如何得到这样的结果blue20  red20  white20
blue21  red21  white21意思就是两个表的两个列的值所有的组合情况!

解决方案 »

  1.   

    select a.color,b.size from table1 a cross join table2 b order by a.color,b.size
      

  2.   

    declare @sql varchar(8000),@int int
    select @sql='',@int=0select
        @int=@int+1,
        @sql=@sql+',[col'+rtrim(@int)+']='''+color+'''+rtrim(size)'
    from
        table1set @sql='select '+stuff(@sql,1,1,'')+' from table2'exec(@sql)
      

  3.   

    SELECT [blue] as col1,[red] as col2,[white] as col3
    FROM 
    (
    SELECT b.Size,a.Color,a.Color+b.Size as c
    FROM tblB as b
     CROSS JOIN tblA AS a
    ) AS p
    PIVOT
    (
    max(c)
    FOR color
    IN ([blue],[red],[white])
    ) as pvt
      

  4.   

    libin_ftsafe
     
    (子陌红尘(retired)) 
    你太强了,太谢谢你了。可是我没分了,呵呵