2014-10-05 80 views
0

我正在嘗試創建一個SQL視圖,它使用內部聯接語句從2個表中獲取信息,但我一直收到一個我找不到的錯誤。我試圖創建的觀點聲明採用名字,姓氏,然後是pid(whats用於鏈接表格),然後僅顯示體重超過140磅的人。當我嘗試在psql中運行我的sql文件時,我不斷收到錯誤。我得到的錯誤是使用內部聯接語句創建sql視圖

\i letsdoit.sql 
output #1 
psql:letsdoit.sql:7: ERROR: column reference "pid" is ambiguous 
LINE 2: SELECT pid,fname, lnam 

我已經是

\echo output #1 
CREATE VIEW weight AS 
SELECT a.pid, a.fname, a.lname 
FROM letsdoit.person as a 
INNER JOIN letsdoit.body_composition as b 
ON a.pid = b.pid 
WHERE (b.weight>140); 

的代碼和我使用的是

        Table "letsdoit.person" 
Column |   Type   |      Modifiers      
--------+-----------------------+--------------------------------------------------- 
pid | integer    | not null default nextval('person_pid_seq'::regclass) 
uid | integer    | 
fname | character varying(25) | not null 
lname | character varying(25) | not null 
Indexes 
"person_pkey" PRIMARY KEY, btree (pid) 
Foreign-key constraints: 
"person_uid_fkey" FOREIGN KEY (uid) REFERENCES university(uid) ON DELETE CASCADE 
Referenced by: 
TABLE "body_composition" CONSTRAINT "body_composition_pid_fkey" FOREIGN KEY (pid 
) REFERENCES person(pid) ON DELETE CASCADE 
TABLE "participated_in" CONSTRAINT "participated_in_pid_fkey" FOREIGN KEY (pid) 
REFERENCES person(pid) 

Table "letsdoit.body_composition" 
Column | Type | Modifiers 
--------+---------+----------- 
pid | integer | not null 
height | integer | not null 
weight | integer | not null 
age | integer | not null 
Indexes: 
"body_composition_pkey" PRIMARY KEY, btree (pid) 
Foreign-key constraints: 
"body_composition_pid_fkey" FOREIGN KEY (pid) REFERENCES person(pid) ON DELETE CASCADE 

回答

1

兩個表你需要指定你正在尋找哪個pid!

替換這樣的:

SELECT a.pid, a.fname, a.lname 
FROM letsdoit.person as a 
INNER JOIN letsdoit.body_composition as b 
ON a.pid = b.pid 
WHERE (b.weight>140); 
+0

@micheal謝謝,我仍然得到一個錯誤,雖然由於某些原因,我在原崗位更新了我的代碼。但是,我得到的錯誤是=> \ i letdoit.sql 輸出#1 psql:letsdoit.sql:7:錯誤:關係「重量」已經存在 – disciples22 2014-10-05 02:06:10

+0

你知道這是爲什麼嗎? – disciples22 2014-10-05 02:14:48

+0

它說錯誤發生在哪一行? – baao 2014-10-05 02:15:47