2014-10-05 104 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