2

我有两张桌子:

tbl_profiles_and_users:

[id] [int] IDENTITY(1,1) NOT NULL,
[profile_id] [int] NOT NULL,
[user_id] [nvarchar](50) NULL

tbl_profiles_and_activities:

[id] [int] IDENTITY(1,1) NOT NULL,
[profile_id] [int] NOT NULL,
[user_id] [nvarchar](50) NOT NULL,
[activity_name] [nvarchar](50) NULL,
[check] [int] NULL,
[preferred] [int] NULL

两个表中都有条目,我试图仅从指定的配置文件中选择用户和活动。

我正在尝试通过以下加入来做到这一点:

use [dbName]

SELECT p1.user_id AS User_ID, [p2.activity_name] as [Activity_Name]
FROM tbl_profiles_and_users AS p1 
INNER JOIN tbl_profiles_and_activities AS p2 
ON p1.user_id = p2.user_id AND p1.profile_id = p2.profile_id

但我收到消息 Invalid column name 'p2.activity_name' 我做错了什么?我可以通过 Join 达到预期的结果吗?

4

1 回答 1

6

因为您正在转义整个列,因此服务器将其读取[p2.activity_name]整个列名,这与[p2].[activity_name]

SELECT p1.user_id AS User_ID, [p2].[activity_name] as [Activity_Name]
FROM ....
于 2012-11-08T15:18:27.653 回答