我有这个查询,它应该返回在特定日期之后创建的数据库中未经验证的帐户的结果。我一直收到这个错误,我不知道如何消除它。这是查询:
select count(*) (nolock)
from dbo.[User]
where ID is not null
and UserStatusID!=2
and CreateDateTime>='5/1/2012'
and not exists (select userid from dbo.UserValidation where dbo.[User].UserID=dbo.UserValidation.UserID)
它出现在"其中dbo。[User] .UserID = dbo.UserValidation.UserID"我在这里做错了什么?
答案 0 :(得分:0)
尝试对表格进行别名处理:
select count(*) (nolock)
from dbo.[User] u
where ID is not null
and UserStatusID != 2
and CreateDateTime >= '5/1/2012'
and not exists (select uv.userid from dbo.UserValidation uv where u.UserID = uv.UserID)
没有架构:
select count(*) (nolock)
from [User] u
where ID is not null
and UserStatusID != 2
and CreateDateTime >= '5/1/2012'
and not exists (select uv.userid from UserValidation uv where u.UserID = uv.UserID)
答案 1 :(得分:0)
在执行JOIN
时,最好明确限定查询中的所有列,如下所示。
select count(u.userid)
from [User] u
where u.ID is not null
and u.UserStatusID != 2
and u.CreateDateTime >= '5/1/2012'
and not exists
(
select uv.userid
from UserValidation uv
where uv.UserID = u.UserID
)