谁能指出以下查询的问题,但我不断收到以下错误:“消息4104,级别16,状态1,行1无法绑定多部分标识符。”
select top 1 *
from [ADatabase].[dbo].[Tracking]
where [ADatabase].[dbo].[Drivers].[DriverId] = [ADatabase].[dbo].[Tracking].[DriverOneId]
and [UTCEventTime] > '2019-02-27'
order by [Id] desc
我看过这个问题的几个示例,但是它们似乎都与更复杂的多表联接查询相关联。
谢谢
答案 0 :(得分:2)
您显然需要某种JOIN
。 。 。和表别名!
select top 1 t.*
from [ADatabase].[dbo].[Tracking] t join
[ADatabase].[dbo].[Drivers] d
on d.[DriverId] = t.[DriverOneId] and
t.[UTCEventTime] > '2019-02-27'
order by t.[Id] desc;
您只能引用FROM
子句中的表中的表和列。您不能只在WHERE
或SELECT
(或其他地方)中添加四部分的标识符,并期望SQL能够理解它。
答案 1 :(得分:0)
错误消息明确指出您需要JOIN
:
SELECT TOP (1) T.*, D.*
from [ADatabase].[dbo].[Tracking] T INNER JOIN
[ADatabase].[dbo].[Drivers] D
ON D.[DriverId] = T.[DriverId]
WHERE T.[UTCEventTime] > '2019-02-27' -- DON'T KNOW ABOUT TABLE WHERE IT COMES FROM
ORDER BY T.[Id] desc; -- DON'T KNOW ABOUT TABLE WHERE IT COMES FROM