创建视图使用多个表

时间:2014-03-18 19:46:34

标签: sql oracle sql-view

您正试图在oracle中创建一个视图。但是我的代码中出现错误,我无法解决如何解决问题。 目前我正在尝试创建一个显示的视图 经理,他们的第一个和最后一个名字也是他们被分配的诊所 诊所的PK和诊所地址的FK 所有地址都存储在该表中(是的,我知道它不是标准的,但这是我选择这样做的方式) 所以我也希望在

显示他们工作的诊所的详细地址

当然这包括两个WHERE语句,我不确定如何实现 第一个是STAFFJOBNAME“MANAGER”

,第二个是ADDRESSNO在两个表上匹配的地方

CREATE VIEW MANAGER AS
    SELECT STAFF.staffno,STAFF.staffFirstName,STAFF.staffLastName,CLINIC.clinicNo, CLINIC.addressNo
    FROM STAFF,CLINIC
    WHERE addressNo = 
    (
        SELECT addressNo, addressStreet, addressCity, addressCounty, addressPostcode, addressTelephone,
        FROM ADDRESS,
        INNER JOIN CLINIC,
        ON ADDRESS.addressNo = CLINIC.addressNo
    ) AND STAFF.staffJobName = 'MANAGER';

我也有这个版本。所以我不确定哪一个更接近正确的那个。

CREATE VIEW MANAGER
(
    AS
    SELECT STAFF.staffno,STAFF.staffFirstName,STAFF.staffLastName,CLINIC.clinicNo, CLINIC.addressNo, ADDRESS.addressNo, ADDRESS.addressStreet, ADDRESS.addressCity, ADDRESS.addressCounty, ADDRESS.addressPostcode, ADDRESS.addressTelephone,
    FROM ADDRESS, STAFF,
    INNER JOIN CLINIC,
    ON ADDRESS.addressNo = CLINIC.addressNo
);

2 个答案:

答案 0 :(得分:6)

CREATE VIEW MANAGERANDCLINIC    AS
    SELECT STAFF.staffno,STAFF.staffFirstName,STAFF.staffLastName,CLINIC.clinicNo, ADDRESS.addressStreet, ADDRESS.addressCity, ADDRESS.addressCounty, ADDRESS.addressPostcode, ADDRESS.addressTelephone
    FROM STAFF,CLINIC, ADDRESS
    WHERE (CLINIC.CLINICMANAGERNO = STAFF.STAFFNO) AND
    (CLINIC.ADDRESSNO = ADDRESS.ADDRESSNO)
    ORDER BY CLINIC.CLINICNO;

最终找到了我自己的问题的答案

答案 1 :(得分:0)

您的第一个视图会出现太多的值错误,因为在您的内部查询中,您返回的是很多列但在where子句中只有一列。

同样在你的第二个代码中,请改变如下

 CREATE VIEW MANAGER

AS
SELECT STAFF.staffno,STAFF.staffFirstName,STAFF.staffLastName,CLINIC.clinicNo, CLINIC.addressNo, ADDRESS.addressNo, ADDRESS.addressStreet, ADDRESS.addressCity, ADDRESS.addressCounty, ADDRESS.addressPostcode, ADDRESS.addressTelephone,
FROM ADDRESS, STAFF
INNER JOIN CLINIC
ON ADDRESS.addressNo = CLINIC.addressNo;