请教一条简单的SQL SERVER里的查询语句问题
表t1/t2/t3 的内容如下(测试用,所以字段简单),关系就是 t1.fid = t2.fid ,而t2 又和t3的关系是 t2.entryid=t3.entryidCreate Table [dbo].[t1]([fid] [int] Null,[a1] [char](10) Null) On [PRIMARY]
Create Table [dbo].[t2]([fid] [int] Null,[entryid] [int] Null,[a1] [nchar](10) Null) On [PRIMARY]
Create Table [dbo].[t3]([nid] [int] Null,[entryid] [int] Null,[a1] [char](10) Null) On [PRIMARY]
insert into t1(fid,a1) values(1,'a1')
insert into t1(fid,a1) values(2,'a1')
insert into t1(fid,a1) values(3,'a1')
insert into t2(fid,entryid,a1) values(1,1,'a1')
insert into t2(fid,entryid,a1) values(1,2,'a1')
insert into t2(fid,entryid,a1) values(1,3,'a1')
insert into t2(fid,entryid,a1) values(1,3,'a1')
insert into t3(nid,entryid,a1) values(1,3,'a1')
insert into t3(nid,entryid,a1) values(2,3,'a1')
我的问题下面的语句执行结果为什么是空,我希望的是只有t1表的全部记录,,因为 t1 是 left join t2的,而t2 和 t3 是INNER 无结果,应该不影响 t1吧? 所以结果应该只有t1的记录啊,为什么实际情况是无记录了, inner 影响的 应该是直接关联的2个表啊,怎么还影响其他表呢?
select * from t1 left join t2 on t2.fid=t1.fid inner join t3 on t3.entryid=t2.entryid and t3.nid=999
麻烦大家帮忙解答一下,谢谢。
难道只能把 t2 和 t3 写成 子查询 再和 t1连接 ??
select * from t1 left join (select t2.fid,t2.entryid,t3.a1 from t2 inner join t3 on t3.entryid=t2.entryid and t3.nid=999) as mx on mx.fid=t1.fid
[此贴子已经被作者于2022-7-29 09:07编辑过]