0

我正在尝试创建一个 SQL 视图,该视图使用内部连接语句从 2 个表中获取信息,但我不断收到一个我无法弄清楚的错误。我尝试创建的视图语句采用名字、姓氏,然后是 pid(用于链接表格的内容),然后仅显示体重超过 140 磅的人。当我尝试在 psql 中运行我的 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
4

1 回答 1

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);
于 2014-10-05T02:00:42.627 回答