创建视图以显示两个以上表中的记录

发布于 2024-10-04 03:58:05 字数 310 浏览 3 评论 0原文

我需要创建一个视图来显示每个员工相关表中的所有记录,并且它还应该显示每个员工以及该特定员工表中的加入日期。我使用了以下代码,但当我尝试运行它时显示错误。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-10-11 03:58:05

您的连接语法不正确。一般来说,要连接两个表 AB,您必须执行以下操作:

select a.*, b.* 
from A a inner join B b on b.a_id = a.id

假设表 A 有一个名为 id 的列 并且表B 有一个名为a_id 的列(可能且最好是外键)。

您可以添加任意数量的内部联接,例如,您可以使用以下方式扩展上述内容:

inner join C on c.b_id = b.id

创建返回所需行的 select 语句后,您可以基于该语句创建视图。

Your join syntax is incorrect. Generally, to join two tables A and B you would have to do the following:

select a.*, b.* 
from A a inner join B b on b.a_id = a.id

Given that table A has a column called id and table B has a column named a_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:

inner join C on c.b_id = b.id

Once you create a select statement that returns the rows you expect, you can create a view based on that statement.

塔塔猫 2024-10-11 03:58:05

您需要使用类似以下内容:

CREATE VIEW EmpDependent AS
    SELECT N.JoinDate
      FROM Lecturer         AS L
      JOIN BranchManager    AS B ON L.EmpRegNo = B.EmpRegNo
      JOIN NonAcademicStaff AS N ON B.EmpRegNo = N.EmpRegNo

但是,您的查询列出了一个在 FROM 子句中未引用的表 LecDependent,并且不清楚为什么讲师也是非学术人员和分行经理(也许讲师是也是自己的依赖)。

您也没有给出任何指示哪个表包含 JoinDate 列 - 我猜是 NonAcademicStaff,但它可能是其中任何一个。如果没有多个过滤条件,您不太可能需要对单个列进行 3 个表连接(或 4 个表连接)。

总的来说,您需要认真考虑在视图中封装的查询。

You need to use something like:

CREATE VIEW EmpDependent AS
    SELECT N.JoinDate
      FROM Lecturer         AS L
      JOIN BranchManager    AS B ON L.EmpRegNo = B.EmpRegNo
      JOIN NonAcademicStaff AS N ON B.EmpRegNo = N.EmpRegNo

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文