日期:2014-05-18  浏览次数:20653 次

sql表关联,多条记录合并为一条记录问题,求救!!
合同表
Id 申请人 经办人 负责人 合同内容
1 01 02 03 埃保常
2 03 03 04 aggressive
3 05 04 05 嘎嘎
4 02 02 02 大厦噶尔外国


人力表
id 姓名
01 张三
02 李四
03 王五
04 小二
05 大傻
......


现在要用合同表关联人力,将同一条合同只显示一次。比如
1 张三 李四 王五 ....
2 王五 王五 小二 ....
......
请问这样在数据库里将如何实现??关联查询语句该如何写。

------解决方案--------------------
SQL code

select  a.id,申请人=b.姓名, 经办人=c.姓名,负责人=d.姓名,a.合同内容
from 合同表 a
left join 人力表  b on a.申请人=b.id 
left join 人力表  c on a.经办人=c.id 
left join 人力表  d on a.负责人=d.id

------解决方案--------------------
SQL code
declare @ta table (id int,va varchar(10))
declare @tb table (id int,vb varchar(10))

insert into @ta select 1,'aa' 
insert into @ta select 2,'bc' 
insert into @ta select 3,'ccc'

insert into @tb select 1,'2'
insert into @tb select 3,'58' 
insert into @tb select 4,'67' 

--内连接简单写法

select a.id,a.va,b.id,b.vb from @ta a,@tb b
where a.id=b.id

--内连接

select a.id,a.va,b.id,b.vb from @ta a inner join @tb b
on a.id=b.id

select a.id,a.va,b.id,b.vb from @ta a join @tb b
on a.id=b.id

--左连接(左外连接)
--返回left join 子句中指定的左表的所有行,以及右表所匹配的行。

select a.id,a.va,b.id,b.vb from @ta a left join @tb b
on a.id=b.id

select a.id,a.va,b.id,b.vb from @ta a left outer join @tb b
on a.id=b.id

--右连接(右外连接)
--返回right join 子句中指定的右表的所有行,以及左表所匹配的行。

select a.id,a.va,b.id,b.vb from @ta a right join @tb b
on a.id=b.id

select a.id,a.va,b.id,b.vb from @ta a right outer join @tb b
on a.id=b.id

--完整外连接
--等同左连接+右连接

select a.id,a.va,b.id,b.vb from @ta a full join @tb b
on a.id=b.id

select a.id,a.va,b.id,b.vb from @ta a full outer join @tb b
on a.id=b.id


--交叉连接
--没有两个表之间关系的交叉连接,将产生连接所涉及的表的笛卡尔积。

select a.id,a.va,b.id,b.vb from @ta a cross join @tb b

select a.id,a.va,b.id,b.vb from @ta a,@tb b

--自连接
--一个表和其本身连接。

select a.id,a.va,b.id,b.va from @ta a,@ta b where a.id=b.id+1

------解决方案--------------------
SQL code


SELECT A.id,B.name AS 申请人,C.name AS 经办人,D.name AS 负责人
FROM 合同表 AS A
    JOIN 人力表 AS B
ON A.申请人=B.id
    JOIN 人力表 AS C
ON A.经办人=C.id
    JOIN 人力表 AS D
ON A.负责人=D.id

------解决方案--------------------
SQL code

---------------------------------
--  Author: htl258(Tony)
--  Date  : 2009-08-02 13:42:15
---------------------------------
--> 生成测试数据表:合同表

If not object_id('[合同表]') is null
    Drop table [合同表]
Go
Create table [合同表]([Id] int,[申请人] int,[经办人] int,[负责人] int,[合同内容] nvarchar(10))
Insert 合同表
Select 1,01,02,03,'埃保常' union all
Select 2,03,03,04,'aggressive' union all
Select 3,05,04,05,'嘎嘎' union all
Select 4,02,02,02,'大厦噶尔外国'
Go
--Select * from 合同表

--> 生成测试数据表:人力表

If not object_id('[人力表]') is null
    Drop table [人力表]
Go
Create table [人力表]([id] int,[姓名] nvarchar(2))
Insert 人力表
Select 01,'张三' union all
Select 02,'李四' union all
Select 03,'王五' union all
Select 04,'小二' union all
Select 05,'大傻'
Go
--Select * from 人力表

-->SQL查询如下:
select a.Id,b.[姓名] [申请人],c.姓名 [经办人],d.姓名 [负责人],合同内容
from 合同表 a
  join 人力表 b
    on a.申请人=b.id
  join 人力表 c
    on a.经办人=c.id
  join 人力表 d
    on a.负责人=d.id
order by 1
/*
Id          申请人  经办人  负责人  合同内容
----------- ---- ---- ---- ----------
1           张三   李四   王五   埃保常
2           王五   王五   小二   aggressive
3           大傻   小二   大傻   嘎嘎
4           李四   李四   李四   大厦噶尔外国

(4 行受影响)

*/

------解决方案--------------------
SQL code

---------------------------------
--  Author: htl258(Tony)
--  Date  : 2009-08-02 13:42:15
---------------------------------
--> 生成测试数据表:合同表

If not object_id('[合同表]') is null
    Drop table [合同表]
Go
Create table [合同表]([Id] int,[申请人] int,[经办人] int,[负责