[英]Creating a sql view with 3 tables
I currently have three tables Engineer, Classes, and Faculty.我目前有三个表工程师、班级和教师。 I am trying to create a view that will display Engineer firstname and lastname, Classes subject and title, and Faculty lastname and email.我正在尝试创建一个视图,该视图将显示工程师的名字和姓氏、课程主题和标题,以及学院的姓氏和 email。 All under one view I've tried multiple different ways but still can't figure out how to properly execute在一个视图下我尝试了多种不同的方法,但仍然无法弄清楚如何正确执行
CREATE VIEW RETRIEVAL
SELECT a.firstname, a.lastname, b.flastname, b.email, c.subject, c.title
FROM engineer a
INNER JOIN faculty b ON a.lastname = b.flastname
INNER JOIN classes c on c.cid = b.fid;
[Edited after clarifications were provided by the asker] [在提问者提供澄清后编辑]
It looks like you want to do something along the lines of:看起来你想做一些类似的事情:
SELECT e.firstname, e.lastname, e.email, f.lastname, c.subject, c.title
FROM engineer e
INNER JOIN faculty f ON e.faculty_id = f.id
INNER JOIN classes c on f.id = c.faculty_id
WHERE c.subject > c.title
This is if you have an id
field in faculty and a faculty_id
field in classes.这是如果您在教员中有一个id
字段,在类中有一个faculty_id
字段。 Also a faculty_id in engineer
也是engineer
的faculty_id
However I'm not sure I understand what do you want to achieve with the c.subject > c.title
condition as it is not mentioned in the question.但是我不确定我是否了解您想通过c.subject > c.title
条件实现什么,因为问题中没有提到它。
This would work for a many-to-one relationship between engineer
and faculty
.这适用于engineer
和faculty
之间的多对一关系。
If there's a many-to-many relationship between engineer
and faculty
then you need an additional table engineer_faculty
where you store the relationships between the two and update the query accordingly如果engineer
和faculty
之间存在多对多关系,那么您需要一个额外的表engineer_faculty
来存储两者之间的关系并相应地更新查询
Before committing to create the VIEW
, run the SELECT
by itself and see if it shows the results that you want.在提交创建VIEW
之前,请自行运行SELECT
并查看它是否显示您想要的结果。 If it does, then create the view.如果是,则创建视图。
Try providing some example data on https://www.mycompiler.io/new/sql We could then be of better help.尝试在https://www.mycompiler.io/new/sql上提供一些示例数据,这样我们可以提供更好的帮助。
You should use the aliases you gave to the tables in the FROM cluase as you did in the WHERE clause:您应该像在 WHERE 子句中那样使用在 FROM 子句中为表指定的别名:
CREATE VIEW RETRIEVAL AS
SELECT a.firstname, a.lastname, b.lastname, b.email, c.subject, c.title
FROM engineer a, faculty b, classes c
WHERE a.lastname = b.lastname
AND c.subject > c.title;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.