select b.userid ,companyname,productname from 会员注册信息 a join 产品信息 b on a.userid=b.userid order by userid

解决方案 »

  1.   

    select a.*,b.produce_name from table1 a,table2 b where a.userid=b.userid
      

  2.   

    select a.user_id,a.company_name,b.product_name from 
    table1 a,table2 b where a.user_id=b.user_id楼主可参考 txlicenhe(马可) 表A,
     id pid
     1   1
     1   2
     1   3
     2   1
     2   2
     3   1
    如何化成表B:
     id pid
      1  1,2,3
      2  1,2
      3  1
    或者是从表B变成A(不要用游标)
    以前有相似的列子,现在找不到了,帮帮忙!
    --1.创建一个合并的函数
    create function fmerg(@id int)
    returns varchar(8000)
    as
    begin
    declare @str varchar(8000)
    set @str=''
    select @str=@str+','+cast(pid as varchar) from 表A where id=@id
    set @str=right(@str,len(@str)-1)
    return(@str)
    End
    go--调用自定义函数得到结果
    select distinct id,dbo.fmerg(id) from 表A
      

  3.   

    select a.*,b.company from  Table2 a left join table1 b on a.user_id=b.user_id
      

  4.   

    select a.user_id,a.company_name,b.product_name
    from table1 a
    left join table2 b
    on a.user_id=b.user_id
    group by a.user_id,a.company_name,b.product_name
    order by a.user_id,a.company_name,b.product_name
      

  5.   

    --如果一定要在SQL中处理,就用:--测试数据
    declare @会员注册信息 table([user_id] varchar(10),company_name varchar(20))
    insert into @会员注册信息
    select 'ooiq','中国软件开发公司'
    union all select 'iiisd','厦门软件公司'
    union all select 'uuuu','中国大水牛公司'declare @产品信息 table([user_id] varchar(10),product_name varchar(20))
    insert into @产品信息
    select 'ooiq','csdn强而有力的oa'
    union all select 'iiisd','xiamen特色版erp'
    union all select 'ooiq','csdn整个论坛产品'
    union all select 'uuuu','大水牛泡泡'
    union all select 'iiisd','msn软件盗版'
    union all select 'iiisd','软件传送带'
    union all select 'ooiq','聊聊吧软件'--查询
    select a,b from(
    select sid1=[user_id],sid2=0 ,a=[user_id]+'会员的',b='公司名称:'+company_name from @会员注册信息
    union all
    select sid1=[user_id],sid2=1,'','该会员有下列这些产品' from @会员注册信息
    union all
    select sid1=[user_id],sid2=2,'',product_name from @产品信息
    )a order by sid1,sid2/*--测试结果a                b                             
    ---------------- ----------------------------- 
    iiisd会员的      公司名称:厦门软件公司
                     该会员有下列这些产品
                     xiamen特色版erp
                     msn软件盗版
                     软件传送带
    ooiq会员的       公司名称:中国软件开发公司
                     该会员有下列这些产品
                     csdn整个论坛产品
                     聊聊吧软件
                     csdn强而有力的oa
    uuuu会员的       公司名称:中国大水牛公司
                     该会员有下列这些产品
                     大水牛泡泡(所影响的行数为 13 行)
    --*/
      

  6.   

    zjcxc(邹建) 的行了,但是像这种形式的报表不适合用sql语句写出来,在前台可能处理起来更方便,比如VB中的mshfgrid中就有合并单元格的功能,而且是自动的,只要你设置了。
      

  7.   

    select a.*,b.produce_name from table1 a,table2 b where a.userid=b.userid