创建视图时,如何从一个表中选择所有内容,从另一个表中选择某些列,
例如我写过
CREATE VIEW NOT_IN_MAN_GLA_LND
AS
SELECT
E.EMPLOYEE_ID, E.TITLE, E.FIRST_NAME, E.LAST_NAME,
E.HOUSE_NO, E.ADDRESS, E.POSTCODE, E.TELE_NO, E.START_DATE,
P.CITY, P.POST_CODE
FROM
EMPLOYEE E, POSTCODE P
WHERE
E.POSTCODE = P.POSTCODE
AND P.CITY NOT IN ('MAN', 'GLA', 'LND');
因此,除了写出employee表中的所有列之外,我可以只选择它们,而仍然只选择邮政编码表中的两列。
答案 0 :(得分:1)
更好的答案是使用正确的,明确的,标准 JOIN
语法:
CREATE VIEW NOT_IN_MAN_GLA_LND AS
SELECT E.*, P.CITY, P.POST_CODE
FROM EMPLOYEE E JOIN
POSTCODE P
ON E.POSTCODE = P.POSTCODE
WHERE P.CITY NOT IN ('MAN', 'GLA', 'LND');
如果您想在结果中包括员工-甚至没有匹配邮政编码的员工-那么:
CREATE VIEW NOT_IN_MAN_GLA_LND AS
SELECT E.*, P.CITY, P.POST_CODE
FROM EMPLOYEE E LEFT JOIN
POSTCODE P
ON E.POSTCODE = P.POSTCODE
WHERE P.CITY NOT IN ('MAN', 'GLA', 'LND') OR
P.CITY IS NULL;
答案 1 :(得分:0)
create or replace VIEW NOT_IN_MAN_GLA_LND
AS SELECT E.*, P.CITY, P.POST_CODE as P_POST_CODE
FROM EMPLOYEE E, POSTCODE P
WHERE E.POSTCODE = P.POSTCODE
AND P.CITY NOT IN ('MAN', 'GLA', 'LND');
for example dept nd emp table:
create or replace view ns_1_v as
select e.*,d.deptno as d_dname,d.dname
from scott.emp e ,scott.dept d
where
e.deptno=d.deptno;
如果两个表中都有一个通用名称,则必须为列名称提供别名,否则,在您的情况下,dupllicaate column name error
会抛出postcode
,因此请为其别名