0

UserName我有两个与字段有关系的表。基于UserName列,我想IDCarReserve表中获取。这是我的 SQL 语句:

SELECT CarReserve.ID 
FROM CarReserve 
 INNER JOIN aspnet_Users ON CarReserve.UserName = aspnet_Users.UserName 
WHERE UserName = @UserName

不幸的是,我收到了这个警告:

不明确的列名“用户名”

谁能告诉我我的陈述有什么问题?

4

3 回答 3

1

您忘记添加 aspnet_users。在 where 子句中的用户名之前。

我也可能建议为您的表使用别名。

于 2012-09-15T15:27:22.933 回答
1

UserName在in之前添加表名WHERE UserName = @UserName

您的查询应该是:

SELECT CarReserve.ID 
    FROM CarReserve 
    INNER JOIN aspnet_Users ON CarReserve.UserName = aspnet_Users.UserName 
    WHERE aspnet_Users.UserName = @UserName
    ------^-----------^----------------

或者

SELECT CarReserve.ID 
    FROM CarReserve 
    INNER JOIN aspnet_Users ON CarReserve.UserName = aspnet_Users.UserName 
    WHERE CarReserve.UserName = @UserName
    ------^---------^----------------
于 2012-09-15T15:27:43.803 回答
1

username您必须为子句中的字段起别名WHERE才能明确限定:

SELECT CarReserve.ID 
FROM CarReserve 
INNER JOIN aspnet_Users ON CarReserve.UserName = aspnet_Users.UserName 
WHERE aspnet_Users.UserName = @UserName
于 2012-09-15T15:28:21.763 回答