34,838
社区成员




CREATE TABLE [d] (
[deptcode] [char] (10) NULL,
[deptname] [char] (10) NULL)
INSERT [d] ([deptcode],[deptname]) VALUES ( '001','部门1')
INSERT [d] ([deptcode],[deptname]) VALUES ( '002','部门2')
INSERT [d] ([deptcode],[deptname]) VALUES ( '003','部门3')
INSERT [d] ([deptcode],[deptname]) VALUES ( '004','部门4')
CREATE TABLE [p] (
[code] [char] (10) NULL,
[deptin] [char] (10) NULL,
[deptout] [char] (10) NULL)
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '01','001','002')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '02','001','003')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '03','002','003')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '04','001','001')
CREATE TABLE [d] (
[deptcode] [char] (10) NULL,
[deptname] [char] (10) NULL)
INSERT [d] ([deptcode],[deptname]) VALUES ( '001','部门1')
INSERT [d] ([deptcode],[deptname]) VALUES ( '002','部门2')
INSERT [d] ([deptcode],[deptname]) VALUES ( '003','部门3')
INSERT [d] ([deptcode],[deptname]) VALUES ( '004','部门4')
CREATE TABLE [p] (
[code] [char] (10) NULL,
[deptin] [char] (10) NULL,
[deptout] [char] (10) NULL)
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '01','001','002')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '02','001','003')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '03','002','003')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '04','001','001')
go
select p.*,d1.deptname as deptname_in,d2.deptname as deptname_out
from p
left join d d1 on p.deptin=d1.deptcode
left join d d2 on p.deptout=d2.deptcode
go
drop table d,p
/*
code deptin deptout deptname_in deptname_out
---------- ---------- ---------- ----------- ------------
01 001 002 部门1 部门2
02 001 003 部门1 部门3
03 002 003 部门2 部门3
04 001 001 部门1 部门1
*/
select p.*,d1.deptname as deptname_in,d2.deptname as deptname_out
from p
left join d as d1 on p.deptin=d1.deptcode
left join d as d2 on p.deptout=d2.deptcode
select p.*,a.deptname as deptname_in,b.deptname as deptname_out
from p
left join d a on p.deptin=a.deptcode
left join d b on p.deptout=b.deptcode
/*
code deptin deptout deptname_in deptname_out
---------- ---------- ---------- ----------- ------------
01 001 002 部门1 部门2
02 001 003 部门1 部门3
03 002 003 部门2 部门3
04 001 001 部门1 部门1
(所影响的行数为 4 行)
*/
CREATE TABLE [d] (
[deptcode] [char] (10) NULL,
[deptname] [char] (10) NULL)
INSERT [d] ([deptcode],[deptname]) VALUES ( '001','部门1')
INSERT [d] ([deptcode],[deptname]) VALUES ( '002','部门2')
INSERT [d] ([deptcode],[deptname]) VALUES ( '003','部门3')
INSERT [d] ([deptcode],[deptname]) VALUES ( '004','部门4')
CREATE TABLE [p] (
[code] [char] (10) NULL,
[deptin] [char] (10) NULL,
[deptout] [char] (10) NULL)
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '01','001','002')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '02','001','003')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '03','002','003')
INSERT [p] ([code],[deptin],[deptout]) VALUES ( '04','001','001')
go
select p.*,d.deptname as deptname_in,d.deptname as deptname_out
from p
left join d on p.deptin=d.deptcode
and p.deptout=d.deptcode
go
drop table d,p
/*
code deptin deptout deptname_in deptname_out
---------- ---------- ---------- ----------- ------------
01 001 002 NULL NULL
02 001 003 NULL NULL
03 002 003 NULL NULL
04 001 001 部门1 部门1
*/