--这个是两条记录合在一起 select A.客户Id,C.[Name],分配时间,成交时间 from 分配表 A inner join 成交表 B on A.客户Id=B.客户Id and A.员工ID=B.员工ID inner join 客户表 C on A.客户Id=C.[ID] Where A.员工ID='100023'--这个不知道是不是你要的结果 select A.客户Id,C.[Name],null 分配时间,成交时间 from 成交表 A inner join 客户表 on A.客户Id=C.[ID] where A.员工ID='100023' union all select A.客户Id,C.[Name],分配时间,null 成交时间 from 分配表 A inner join 客户表 on A.客户Id=C.[ID] where A.员工ID='100023' --这个也可以转换成 select * from ( select A.客户Id,C.[Name],null 分配时间,成交时间,A.员工ID from 成交表 A inner join 客户表 on A.客户Id=C.[ID] union all select A.客户Id,C.[Name],分配时间,null 成交时间,A.员工ID from 分配表 A inner join 客户表 on A.客户Id=C.[ID] ) T on T.员工ID='100023'
这样不行 null是查询出来的,不是自己写上去的。呵呵
--> 测试数据: [客户表] if object_id('[客户表]') is not null drop table [客户表] create table [客户表] (ID int,name varchar(4),age int) insert into [客户表] select 1,'张三',22 union all select 2,'李四',33 union all select 3,'王五',21 union all select 4,'赵六',41 --> 测试数据: [分配表] if object_id('[分配表]') is not null drop table [分配表] create table [分配表] (客户Id int,分配时间 datetime,员工ID int) insert into [分配表] select 2,'2010-11-1',100023 union all select 3,'2010-11-1',100024 --> 测试数据: [成交表] if object_id('[成交表]') is not null drop table [成交表] create table [成交表] (客户Id int,成交时间 datetime,员工ID int) insert into [成交表] select 1,'2010-11-4',1000023 union all select 4,'2010-11-5',1000024select t1.ID as customerID,t1.name as 客户姓名,t2.分配时间,t3.成交时间 from [客户表] t1 left join [分配表] t2 on t1.ID=t2.客户Id left join [成交表] t3 on t1.ID=t3.客户Id/* customerID 客户姓名 分配时间 成交时间 1 张三 NULL 2010-11-04 00:00:00.000 2 李四 2010-11-01 00:00:00.000 NULL 3 王五 2010-11-01 00:00:00.000 NULL 4 赵六 NULL 2010-11-05 00:00:00.000 */
--> 测试数据: [客户表] if object_id('[客户表]') is not null drop table [客户表] create table [客户表] (ID int,name varchar(4),age int) insert into [客户表] select 1,'张三',22 union all select 2,'李四',33 union all select 3,'王五',21 union all select 4,'赵六',41 --> 测试数据: [分配表] if object_id('[分配表]') is not null drop table [分配表] create table [分配表] (客户Id int,分配时间 datetime,员工ID int) insert into [分配表] select 2,'2010-11-1',100023 union all select 3,'2010-11-1',100024 --> 测试数据: [成交表] if object_id('[成交表]') is not null drop table [成交表] create table [成交表] (客户Id int,成交时间 datetime,员工ID int) insert into [成交表] select 1,'2010-11-4',100023 union all select 4,'2010-11-5',100024 select t1.ID as customerID,t1.name as 客户姓名,t2.分配时间,t3.成交时间 from [客户表] t1 left join [分配表] t2 on t1.ID=t2.客户Id left join [成交表] t3 on t1.ID=t3.客户Id where t2.员工ID=100023 or t3.员工ID=100023 /* customerID 客户姓名 分配时间 成交时间 1 张三 NULL 2010-11-04 00:00:00.000 2 李四 2010-11-01 00:00:00.000 NULL */
--这个是两条记录合在一起
select A.客户Id,C.[Name],分配时间,成交时间 from 分配表 A
inner join 成交表 B on A.客户Id=B.客户Id and A.员工ID=B.员工ID
inner join 客户表 C on A.客户Id=C.[ID]
Where A.员工ID='100023'--这个不知道是不是你要的结果
select A.客户Id,C.[Name],null 分配时间,成交时间 from 成交表 A inner join 客户表 on A.客户Id=C.[ID] where A.员工ID='100023'
union all
select A.客户Id,C.[Name],分配时间,null 成交时间 from 分配表 A inner join 客户表 on A.客户Id=C.[ID] where A.员工ID='100023'
--这个也可以转换成
select * from (
select A.客户Id,C.[Name],null 分配时间,成交时间,A.员工ID from 成交表 A inner join 客户表 on A.客户Id=C.[ID]
union all
select A.客户Id,C.[Name],分配时间,null 成交时间,A.员工ID from 分配表 A inner join 客户表 on A.客户Id=C.[ID]
) T on T.员工ID='100023'
if object_id('[客户表]') is not null drop table [客户表]
create table [客户表] (ID int,name varchar(4),age int)
insert into [客户表]
select 1,'张三',22 union all
select 2,'李四',33 union all
select 3,'王五',21 union all
select 4,'赵六',41
--> 测试数据: [分配表]
if object_id('[分配表]') is not null drop table [分配表]
create table [分配表] (客户Id int,分配时间 datetime,员工ID int)
insert into [分配表]
select 2,'2010-11-1',100023 union all
select 3,'2010-11-1',100024
--> 测试数据: [成交表]
if object_id('[成交表]') is not null drop table [成交表]
create table [成交表] (客户Id int,成交时间 datetime,员工ID int)
insert into [成交表]
select 1,'2010-11-4',1000023 union all
select 4,'2010-11-5',1000024select t1.ID as customerID,t1.name as 客户姓名,t2.分配时间,t3.成交时间
from [客户表] t1 left join [分配表] t2 on t1.ID=t2.客户Id
left join [成交表] t3 on t1.ID=t3.客户Id/*
customerID 客户姓名 分配时间 成交时间
1 张三 NULL 2010-11-04 00:00:00.000
2 李四 2010-11-01 00:00:00.000 NULL
3 王五 2010-11-01 00:00:00.000 NULL
4 赵六 NULL 2010-11-05 00:00:00.000
*/
if object_id('[客户表]') is not null drop table [客户表]
create table [客户表] (ID int,name varchar(4),age int)
insert into [客户表]
select 1,'张三',22 union all
select 2,'李四',33 union all
select 3,'王五',21 union all
select 4,'赵六',41
--> 测试数据: [分配表]
if object_id('[分配表]') is not null drop table [分配表]
create table [分配表] (客户Id int,分配时间 datetime,员工ID int)
insert into [分配表]
select 2,'2010-11-1',100023 union all
select 3,'2010-11-1',100024
--> 测试数据: [成交表]
if object_id('[成交表]') is not null drop table [成交表]
create table [成交表] (客户Id int,成交时间 datetime,员工ID int)
insert into [成交表]
select 1,'2010-11-4',100023 union all
select 4,'2010-11-5',100024
select t1.ID as customerID,t1.name as 客户姓名,t2.分配时间,t3.成交时间
from [客户表] t1 left join [分配表] t2 on t1.ID=t2.客户Id
left join [成交表] t3 on t1.ID=t3.客户Id
where t2.员工ID=100023 or t3.员工ID=100023
/*
customerID 客户姓名 分配时间 成交时间
1 张三 NULL 2010-11-04 00:00:00.000
2 李四 2010-11-01 00:00:00.000 NULL
*/