简体   繁体   English

从一个表中选择所有内容,从另一个SQL中选择某些列

[英]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.

相关问题 从一个表中选择所有内容,并从另一个表中选择数组 - select everything from one table, and array from another SQL选择一个表中的所有列和另一个表上另一列的最大值 - SQL Select all columns from one table and Max value of another column on another table SQL从一个表中选择两列,由另一表中的列翻译 - SQL Select two columns from one table translated by a column from another table SQL 根据另一个表中的两列有效地从一个表中选择不同的行 - SQL efficiently select distinct rows from one table depending on two columns from another table SQL从一个表中选择不在另一个表中的项 - SQL select item from one table that is not in another Select 一个表的所有列和另一个表的一些列 - Select all columns from one table and some from another table 如何在SQL中将某些列从一个表移动到另一个表? - How do I move certain columns from one table to another in SQL? SQL:select 来自某个表的行,基于此表和另一个表中的条件 - SQL: select rows from a certain table based on conditions in this and another table 如何从一个表中选择两列连接到SQL中另一表中的列? - How to select two columns from one table that are connected to a column in another table in SQL? SQL Server:无法通过从同一数据库问题上的另一个表中过滤来选择一个表的列 - SQL Server : can't select columns of one table by filtering from another table on same database issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM