一个表关联查询问题【100分求助】?
表1:------------------------------ 【人事表】
表名:【Man】
字段: Man_Id,Man_Name,Zw_Id
1 张飞 2
2 曹操 1
表2:------------------------------ 【职务表】
表名:【Zw】
字段: Zw_Id,Zw_Name
1 校长
2 班主任
表3:------------------------------ 【工资表】
表名:【Zw】
字段: Man_Id,Money
1 2500
2 4000
需要的------------------------------- 【查询结果】:
Man_Id,Man_Name,Zw_Id,Zw_Name,Money
1 张飞 2 班主任 2500
2 曹操 1 校长 4000
------------------------------------------ 【要求】
1、一条Sql语句
2、而且是Select语句,不用With
3、愿出100分谢谢各位!
[解决办法]
SELECT A.MAN_ID,A.MAN_NAME,A.ZW_ID,B.ZW_NAME,C.MONEY FROM MAN A
LEFT JOIN ZW B ON A.ZW_ID=B.ZW_ID
LEFT JOIN GZ C ON A.MAN_ID=C.MAN_ID
----------------------------
-- Author :TravyLee(物是人非事事休,欲语泪先流!)
-- Date :2012-12-11 10:35:32
-- Version:
-- Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86)
--Feb 10 2012 19:13:17
--Copyright (c) Microsoft Corporation
--Enterprise Edition: Core-based Licensing on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[Man]
if object_id('[Man]') is not null drop table [Man]
go
create table [Man]([Man_Id] int,[Man_Name] varchar(4),[Zw_Id] int)
insert [Man]
select 1,'张飞',2 union all
select 2,'曹操',1
--> 测试数据:[Zw]
if object_id('[Zw]') is not null drop table [Zw]
go
create table [Zw]([Zw_Id] int,[Zw_Name] varchar(6))
insert [Zw]
select 1,'校长' union all
select 2,'班主任'
--> 测试数据:[Gz]
if object_id('[Gz]') is not null drop table [Gz]
go
create table [Gz]([Man_Id] int,[Money] int)
insert [Gz]
select 1,2500 union all
select 2,4000
go
select
a.Man_Id,
Man_Name,
b.Zw_Id,
Zw_Name,
Money
from
[Man] a
inner join
[Zw] b
on
a.Zw_Id=b.Zw_Id
inner join
[Gz] c
on
a.Man_Id=c.Man_Id
/*
Man_IdMan_NameZw_IdZw_NameMoney
-----------------------------------------------------
1张飞2班主任2500
2曹操1校长4000
*/
select
Man_Id,Man_Name,sex_name,Zw_Name,Money
from(
select
a.Man_Id,
Man_Name,
b.Zw_Id,
Zw_Name,
Money ,
Sex_Id
from
[Man] a
inner join
[Zw] b
on
a.Zw_Id=b.Zw_Id
inner join
[Gz] c
on
a.Man_Id=c.Man_Id
)t
inner join xb m
on m.Sex_Id=t.Sex_Id