2012-11-08 57 views
2

獲取值時,我有兩個表:無效列名稱中使用INNER JOIN從兩個表中

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 

,但我收到消息 無效的列名稱p2.activity_name「 什麼我做錯了什麼?我可以通過加入來達到預期效果嗎?

回答

6

,因爲你在逃避整列,因此,服務器把它讀成[p2.activity_name]整個列名是從[p2].[activity_name]

SELECT p1.user_id AS User_ID, [p2].[activity_name] as [Activity_Name] 
FROM .... 
+3

(實際上是['分隔identifier'(HTTP很大的不同: //msdn.microsoft.com/en-us/library/aa224033(v=sql.80).aspx),不會轉義。) – RedFilter

+0

謝謝,這是解決方案。當定時器允許時,我會接受你的答案。 – Christian

+0

@RedFilter是的,你是對的。試圖找出正確的術語。謝謝! –