如何左连接查询?

yumanqing 2008-09-12 03:22:38
创建测试环境脚本

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')





-----------------------
查询语句
select p.*,d.deptname as deptname_in,d.deptname as deptname_out
from p
left join d on p.deptin=d.deptcode
left join d on p.deptout=d.deptcode

p表中deptin是发出部门,deptout是接收部门,我想查询这连个部门编码的名称,上面的语句报错,另外,p表只是一个模拟,实际这个表有很多要与基础档案表连接多次的字段,比如发出仓库编码,接收仓库编码,发料人编码,收料人编码,查询语句该怎么写,谢谢..等待中.不解决中秋节都过不好呀
...全文
436 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanjoe109 2008-11-12
  • 打赏
  • 举报
回复
哈哈,今天用上了!
hanjoe109 2008-09-30
  • 打赏
  • 举报
回复
看懂了,呵呵
yumanqing 2008-09-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lgxyz 的回复:]
引用 5 楼 yumanqing 的回复:
楼上明显是错误的,你看deptin,deptname_in都不对应


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 …
[/Quote]
这个是对的,
lgxyz 2008-09-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yumanqing 的回复:]
楼上明显是错误的,你看deptin,deptname_in都不对应
[/Quote]

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

这什么个不对啊
-晴天 2008-09-12
  • 打赏
  • 举报
回复
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

*/
csdyyr 2008-09-12
  • 打赏
  • 举报
回复


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
yumanqing 2008-09-12
  • 打赏
  • 举报
回复
楼上明显是错误的,你看deptin,deptname_in都不对应
lgxyz 2008-09-12
  • 打赏
  • 举报
回复
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 行)
*/
zoujp_xyz 2008-09-12
  • 打赏
  • 举报
回复
合起来写:
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
-晴天 2008-09-12
  • 打赏
  • 举报
回复
是要的这个吗?

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
*/
liangCK 2008-09-12
  • 打赏
  • 举报
回复
select p.*,d.deptname as deptname_in,d.deptname as deptname_out
from p
left join d on p.deptin=d.deptcode
left join d as d1 on p.deptout=d1.deptcode

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧