2010-07-12 105 views
8

我有以下SQL和它引發錯誤不明確的列名「ID」不明確的列名

select tbl_registration.*, tbl_ebp.name as ebp_name, tbl_Users.id as user_id, tbl_ebp.id as linked_ebp_id 
from tbl_registration 
left outer join tbl_ebp on tbl_ebp.id = tbl_registration.ebp_id 
left outer join tbl_users on tbl_registration.email = tbl_users.username 
where id = [PARAM]p_id 

我讀過這方面的一些文章,但無法找到我的代碼工作解決方案。 任何幫助非常感謝。

回答

21

你的WHERE子句ID需要更加具體,包括表名:

WHERE table.id = [PARAM]p_id 

如果兩個東西共享相同的名字,這是其中的模糊性步驟在這種情況下,多個表中的SQL。包含「id」列。

如果列名在當前正在觸摸的一組表中是唯一的,那麼SQL有消除列名歧義的智能 - 因此大多數情況下,您不需要在列名前添加表名。

+2

我個人更喜歡總是指定字段來自哪個表。這使得維護更容易,尤其是在處理加入十個不同表格的複雜報告類型查詢時。這樣我就知道這個領域從哪裏來,如果它是給我一個問題而不必查找十個不同表格的結構來找出它來自哪裏的問題。 – HLGEM 2010-07-12 19:01:55

6

最有可能多於一個表具有名爲id的列;在where子句中使用表前綴

4

您是否曾嘗試在where子句中爲id列名添加前綴?

2

它指的是你的where子句中的「id」。你需要指定它應該過濾哪個表的「id」。

0

我已經取代你的代碼

select *, tbl_ebp.name as ebp_name, tbl_Users.id as user_id, tbl_ebp.id as linked_ebp_id 
from tbl_registration 
left outer join tbl_ebp on tbl_ebp.id = tbl_registration.ebp_id 
left outer join tbl_users on tbl_registration.email = tbl_users.username 
where your_respective_tblname.id = your_respective_tblname.[PARAM]p_id