有两个表如下,要求输出入下的查询结果,sql语句怎样写呢?
表1:
id name1 张三
2 李四
3 王五表2:
id kind good1 A类 电扇
1 A类 电吹风
1 B类 橙汁
2 B类 汽水
2 C类 牙膏
2 C类 牙刷
3 B类 牛奶
3 B类 汽水
查询结果:|----------------------------------------------------|
|id | name |    A类       |   B类       |  C类       |
|---|------|--------------|-------------|------------|
| 1 | 张三 | 电扇,电吹风 |  橙汁       |            |
|---|------|--------------|-------------|------------|
| 2 | 李四 |              |  汽水       | 牙膏,牙刷 |
|---|------|--------------|-------------|------------|
| 3 | 王五 |              |  牛奶,汽水 |            |
|----------------------------------------------------|

解决方案 »

  1.   

    --建立测试环境 
    Create Table 表1(组名 varchar(10),成员1id varchar(10),成员2id varchar(10),成员3id varchar(10)) 
    --插入数据 
    insert into 表1 
    select '冲锋组','1','2','3' union 
    select '后卫组','2','3','4' 
    Create Table 表2(成员id varchar(10),成员姓名 varchar(10)) 
    --插入数据 
    insert into 表2 
    select '1','张三' union 
    select '2','李四' union 
    select '3','王五' union 
    select '4','陆二' --测试语句 
    select a.组名, 
    成员1=(select 成员姓名 from 表2 b where a.成员1id=b.成员id), 
    成员1=(select 成员姓名 from 表2 b where a.成员2id=b.成员id), 
    成员1=(select 成员姓名 from 表2 b where a.成员3id=b.成员id) 
    from 表1 a 
    --删除测试环境 
    Drop Table 表1 
    Drop Table 表2 /* 
    组名 成员1 成员1 成员1 
    ---------- ---------- ---------- ---------- 
    冲锋组 张三 李四 王五 
    后卫组 李四 王五 陆二 (所影响的行数为 2 行) 
    */
      

  2.   

    to:(SQL新手) 
    这个情况不一样,你的例子用在我的表上会报错,“子查询不止一个.....”
      

  3.   

    ---测试数据---
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([id] int,[name] varchar(4))
    insert [ta]
    select 1,'张三' union all
    select 2,'李四' union all
    select 3,'王五'
    go
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[kind] varchar(3),[good] varchar(6))
    insert [tb]
    select 1,'A类','电扇' union all
    select 1,'A类','电吹风' union all
    select 1,'B类','橙汁' union all
    select 2,'B类','汽水' union all
    select 2,'C类','牙膏' union all
    select 2,'C类','牙刷' union all
    select 3,'B类','牛奶' union all
    select 3,'B类','汽水'
    go---字符连接函数---
    create function f_kind(@id int,@kind varchar(20))
    returns varchar(30)
    as 
    begin
      declare @s varchar(30)
      select @s=isnull(@s+',','')+good from tb where id=@id and kind=@kind
      return @s
    end
    go---查询---
    select 
      a.id,
      a.name,
      [A类]=max(dbo.f_kind(a.id,'A类')),
      [B类]=max(dbo.f_kind(a.id,'B类')),
      [C类]=max(dbo.f_kind(a.id,'C类'))
    from
      ta a,tb b
    where
      a.id=b.id
    group by
      a.id,a.name
    ---结果---
    id          name A类                             B类                             C类
    ----------- ---- ------------------------------ ------------------------------ ------------------------------
    1           张三   电扇,电吹风                         橙汁                             NULL
    2           李四   NULL                           汽水                             牙膏,牙刷
    3           王五   NULL                           牛奶,汽水                          NULL(3 行受影响)