[英]Select everything from one table, and certain columns from another SQL
How can I select everything from one table and certain columns from another when creating a view, 创建视图时,如何从一个表中选择所有内容,从另一个表中选择某些列,
For example I've wrote 例如我写了
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');
So instead of writing all the column from the employee table is there anyway I can just select them all while still selecting only the two from the postcode table. 因此,无论如何,我没有写出employee表中的所有列,而是只选择了它们,而仍然只从邮政编码表中选择了两列。
The better answer is to use proper, explicit, STANDARD JOIN
syntax: 更好的答案是使用正确的,显式的STANDARD
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');
If you wanted to include employees in the results -- even those who have no matching postal code -- then: 如果您想在结果中包括员工-甚至没有匹配邮政编码的员工-那么:
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;
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;
you have to give aliases for the column names where there is a common name in both the tables otherwise it will throw dupllicaate column name error
in your case postcode
is common so give alias for it 您必须为两个表中都有通用名称的列名称提供别名,否则在您的
postcode
常见的情况下,它将抛出dupllicaate column name error
,因此请为其提供别名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.