创建视图以显示两个以上表中的记录
我需要创建一个视图来显示每个员工相关表中的所有记录,并且它还应该显示每个员工以及该特定员工表中的加入日期。我使用了以下代码,但当我尝试运行它时显示错误。
CREATE VIEW EmpDependent
AS
SELECT JoinDate
FROM Lecturer,BranchManager,NonAcademicStaff
INNER JOIN LecDependent.EmpRegNo=Lecturer.EmpRegNo=BranchManager.EmpRegNo=NonAcademicStaff.EmpRegNo
请帮忙
I need to create a view to display all the records from each of the employee dependent tables, and also it should show the join date of every employee as well from that particular employee's table. I used the following code, but it shows an error when I try to run it.
CREATE VIEW EmpDependent
AS
SELECT JoinDate
FROM Lecturer,BranchManager,NonAcademicStaff
INNER JOIN LecDependent.EmpRegNo=Lecturer.EmpRegNo=BranchManager.EmpRegNo=NonAcademicStaff.EmpRegNo
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的连接语法不正确。一般来说,要连接两个表
A
和B
,您必须执行以下操作:假设表
A
有一个名为id 的列
并且表B
有一个名为a_id
的列(可能且最好是外键)。您可以添加任意数量的内部联接,例如,您可以使用以下方式扩展上述内容:
创建返回所需行的
select
语句后,您可以基于该语句创建视图。Your join syntax is incorrect. Generally, to join two tables
A
andB
you would have to do the following:Given that table
A
has a column calledid
and tableB
has a column nameda_id
(possibly and preferably being a foreign key).You can add as many inner joins as like, for example you could extend the above with:
Once you create a
select
statement that returns the rows you expect, you can create a view based on that statement.您需要使用类似以下内容:
但是,您的查询列出了一个在 FROM 子句中未引用的表 LecDependent,并且不清楚为什么讲师也是非学术人员和分行经理(也许讲师是也是自己的依赖)。
您也没有给出任何指示哪个表包含 JoinDate 列 - 我猜是 NonAcademicStaff,但它可能是其中任何一个。如果没有多个过滤条件,您不太可能需要对单个列进行 3 个表连接(或 4 个表连接)。
总的来说,您需要认真考虑在视图中封装的查询。
You need to use something like:
However, your query lists a table LecDependent that is not cited in the FROM clause, and it is not clear why a lecturer is also a non-academic staff member and a branch manager (and maybe a lecturer is also their own dependent).
You also didn't give any indication of which table contains the JoinDate column - I guessed NonAcademicStaff, but it could be any of them. And you'd be unlikely to need a 3 table join (or a 4 table join) for a single column without a number of filter conditions.
Overall, you need to think hard about the query you are encapsulating in the view.