sub select 内部 jon "from tb1 t inner join (select a, b from tb2 where b = tb1.item2) as t2 on t2.a = tb1.b"

sub select inner jon "from tb1 t inner join (select a, b from tb2 where b = tb1.item2) as t2 on t2.a = tb1.b"

美好的一天 我有以下查询

SELECT 
    G.guid, G.guLastName1, G.guFirstName1, G.guShowD,
    G.guPRInvit1 as guPR1, GP1.peN as guPR1N, pl.pppo  as POST,
    pl.pppe, pl.pppo
FROM Guests G 
left join Personnel GP1 on GP1.peID = G.guPRInvit1
LEFT JOIN (SELECT TOP 1 a.pppo, a.ppDT, a.pppe FROM PostsLog a where a.pppe = GP1.peID ) as pl
ON (pl.pppe = GP1.peID) --and DATEDIFF(DAY,pl.ppDT,G.guShowD)>=0)
WHERE 
G.guShowD between @datefrom and @DateTo and G.gusr = @SalesRoom

table PostsLog 是 table 人员的位置日志,然后我需要日期范围内的人员位置

但是当 运行 我得到以下错误

The multi-part identifier "GP1.peID " could not be bound.

有人可以告诉我我做错了吗?我不太擅长 SQL 服务器,找不到我的错误

删除子查询并将条件移动到 on 子句:

select tb1.a, tb1.b, tb1.n, t2.a, t2.b 
from table1 tb1 inner join
     table2 t2
     on t2.b = tb1.n and t2.c = tb1.b
where 1 = 1 ;

您还有另一个错误,缺少 tbl1.nt2.a 之间的逗号,因为列别名中不允许(未转义)句点。此外,where 子句是多余的;我猜您正在用代码构建查询。

这似乎是你真正的问题:

                                                              -------VV
LEFT JOIN (SELECT TOP 1 a.pppo, a.ppDT, a.pppe FROM PostsLog a where pl.pppe = GP1.peID ) as pl

pl 别名用于子查询本身,因此不能在 子查询

中使用

我怀疑你的意思是

                                                              -------V
LEFT JOIN (SELECT TOP 1 a.pppo, a.ppDT, a.pppe FROM PostsLog a where a.pppe = GP1.peID ) as pl