我现在有三个表create table User
(
uId int,
uName varchar(20)
)create table TableType
(
tId int,
tName varchar(50)
)
create table [Table]
(
Id int ,
uName varchar(20),
tId int,
uId int
)
go
insert into User values(1,'张三')
insert into User values(2,'李四')
insert into TableType values(1,'电表')
insert into TableType values(2,'水表')
insert into TableType values(3,'燃气表')
insert into Table values(1,'张三的电表',1,1)
insert into Table values(1,'张三的水表',1,2)
insert into Table values(1,'李四的电表',2,1)
insert into Table values(1,'李四的燃气表',2,3)
现在我需要一个结果集(视图)
输出效果如下姓名 表类型 表名
张三  电表   张三的电表
张三  水表   张三的水表
张三  燃气表  null       << 因为没有插入 这里我想通过外联结实现 可是搞不定  貌似需要表类型分组 求高人指点
李四  电表  李四的电表
李四  水表   null
李四  燃气表 李四的燃气表 

解决方案 »

  1.   

    create table [User]
    (
    uId int,
    uName varchar(20)
    )create table TableType
    (
    tId int,
    tName varchar(50)
    )
    create table [Table]
    (
    Id int ,
    uName varchar(20),
    tId int,
    uId int
    )
    go
    insert into [User] values(1,'张三')
    insert into [User] values(2,'李四')
    insert into TableType values(1,'电表')
    insert into TableType values(2,'水表')
    insert into TableType values(3,'燃气表')
    insert into [Table] values(1,'张三的电表',1,1)
    insert into [Table] values(1,'张三的水表',1,2)
    insert into [Table] values(1,'李四的电表',2,1)
    insert into [Table] values(1,'李四的燃气表',3,2)--你的数据有问题,改了下
    select a.uName,b.tName,c.uName from [User] a 
     left join TableType b on 1=1
    left join [Table] c on b.tId=c.tId and a.uId=c.uId/*
    uName                tName                                              uName
    -------------------- -------------------------------------------------- --------------------
    张三                   电表                                                 张三的电表
    张三                   水表                                                 李四的电表
    张三                   燃气表                                                NULL
    李四                   电表                                                 张三的水表
    李四                   水表                                                 NULL
    李四                   燃气表                                                李四的燃气表
      

  2.   


    select A.uName, tName, case when C.Id IS null then null else A.uName+'的'+A.tName end from (select uid, tid, tName, uName from [User], [TableType]) A left join [Table] C on A.uid=C.uId and A.tId=C.tId
    uName                tName                                              
    -------------------- -------------------------------------------------- ------------------------------------------------------------------------
    张三                   电表                                                 张三的电表
    张三                   水表                                                 张三的水表
    张三                   燃气表                                                NULL
    李四                   电表                                                 李四的电表
    李四                   水表                                                 NULL
    李四                   燃气表                                                NULL