Mysql 使用内部join语句创建sql视图
我试图创建一个SQL视图,它使用一个内部join语句从两个表中获取信息,但我一直遇到一个无法解决的错误。我正在尝试创建的view语句采用名字、姓氏,然后是pid(用来链接表格的内容),然后只显示体重超过140磅的人。当我尝试在psql中运行我的sql文件时,我总是遇到一个错误。我得到的错误是Mysql 使用内部join语句创建sql视图,mysql,sql,view,create-view,Mysql,Sql,View,Create View,我试图创建一个SQL视图,它使用一个内部join语句从两个表中获取信息,但我一直遇到一个无法解决的错误。我正在尝试创建的view语句采用名字、姓氏,然后是pid(用来链接表格的内容),然后只显示体重超过140磅的人。当我尝试在psql中运行我的sql文件时,我总是遇到一个错误。我得到的错误是 \i letsdoit.sql output #1 psql:letsdoit.sql:7: ERROR: column reference "pid" is ambiguous LINE 2: SELE
\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)
及
您需要指定要查找的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);
@micheal谢谢,我仍然收到一个错误,但是出于某种原因,我在原始帖子中更新了我的代码。但是我得到的错误是=>\I letsdoit.sql输出#1 psql:letsdoit.sql:7:error:relationship“weight”已经存在你知道为什么吗?它在第7行。错误是psql:letsdoit.sql:7:error:relationship“weight”已经存在–它可能与主键和外键有关?我不这么认为,但只能猜测。你能这样试试吗:其中b.weight>140//不带()
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);