• 主页
• 基础类
• 应用实例
• 新技术前沿

# 有三个主表,三个主表之间有多对多表, 如何联接

Crystal_Boy 2007-12-26 02:38:12
a表 aid,aname (a主表)
b表 aid,cid (存储a表与b表的多对多关系)
c表 cid,cname (c主表)
d表 cid,eid (存储c表与e表的多对多关系)
e表 eid,ename (e主表)

select a.*, b.*, c.* from
a inner join
(b inner join c
on b.cid = c.cid)
on a.aid = b.aid

select c.*, d.*, e.* from
c inner join
(d inner join e on d.eid = e.eid )
on c.cid = d.eid

select a.*, c.*, e.* from
a inner join
(b inner join
(c inner join
(d inner join e on d.eid = e.eid )
on c.cid = d.eid)
on b.cid = c.cid)
on a.aid = b.aid
...全文
76 点赞 收藏 9

9 条回复

laiqun123 2007-12-27

Crystal_Boy 2007-12-27

Crystal_Boy 2007-12-26
a表: b表: c表 d表 e表
aid,aname aid,cid cid,canme cid,eid eid,ename
1 张三 1 2 1 小编 1 2 1 删除新闻贴
2 李四 1 3 2 总编 2 1 2 发新闻贴
3 王五 2 1 3 经理 2 2 3 加奖金
3 1 3 3

Crystal_Boy 2007-12-26
a表 aid,aname (a主表)
b表 aid,cid (存储a表与b表的多对多关系)
c表 cid,cname (c主表)
d表 cid,eid (存储c表与e表的多对多关系)
e表 eid,ename (e主表)

a表: b表: c表 d表 e表
aid,aname aid,cid cid, canme cid, eid eid,ename
1 张三 1 1 小编 1 2 1 删除新闻贴
2 李四 1 2 总编 2 1 2 发新闻贴
3 王五 2 3 经理 2 2 3 加奖金
3 3 3

``````
select   a.*,   c.*,   e.*   from
a   inner   join
(b   inner   join
(c   inner   join
(d   inner   join   e   on   d.eid   =   e.eid   )   <<-- 嵌套这行是记录就不完了,请问为什么
on   c.cid   =   d.eid)
on   b.cid   =   c.cid)
on   a.aid   =   b.aid

``````

Crystal_Boy 2007-12-26
``````
select a.*,c.* from a
inner join b on a.aid=b.aid
inner join c on b.cid=c.cid
inner join d on c.cid=d.eid <--- 内联d表时,无记录(d表为c表与e表的多对多关系表)
``````

Crystal_Boy 2007-12-26

liangCK 2007-12-26

``````select a.*,c.*,e.* from
a inner join b on a.aid=b.aid inner join c on b.cid=c.cid
inner join d on c.cid=d.eid inner join e on d.eid=e.eid``````

liangCK 2007-12-26
``````select a.*,c.*,e.* from
a inner join b on a.aid=b.aid innser join c on b.cid=c.cid
innser join d on c.cid=d.eid inner join e on d.eid=e.eid``````

MS-SQL Server

3.2w+

MS-SQL Server相关内容讨论专区