oracle 使用来自 2 个不同表的字段创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1163082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:38:48  来源:igfitidea点击:

Creating tables with fields from 2 different tables

sqloracleoracle10g

提问by novice

I want to create a table that stores values from two different tables;

我想创建一个表来存储来自两个不同表的值;

From table 1: cust_id (varchar2), invoice_amt (float)

From table 2: cust_id (from table 1), payment_date

来自表 1:cust_id (varchar2), invoice_amt (float)

来自表 2:cust_id(来自表 1),payment_date

My table should have 3 fields:

我的表应该有 3 个字段:

cust_id, invoice_amt, payment_date

I tried the following, which is obviously wrong.

我尝试了以下,这显然是错误的。

create table temp1 as (
    select table_1.cust_id, table_1.invoice_amt, table_2.payment_date
      from table_1@dblink, table_2@dblink)

Your valuable suggestions will be of great help.

您的宝贵建议将大有帮助。

回答by inkedmn

create table temp1 as (
    select 
        table_1.cust_id,
        table_1.invoice_amt,
        table_2.payment_date 
    from 
        table_1@dblink, 
        table_2@dblink 
    where 
        table_1.cust_id = table_2.cust_id
    )

I'm no oracle guy, but that should do what you want (untested, though).

我不是 oracle 人,但这应该做你想做的事(虽然未经测试)。

回答by Ogre Codes

You were close:

你很接近:

create table temp1 as ( 
    select t1.cust_id, t1.invoice_amt, t2.payment_date 
      from table_1@dblink t1, table_2@dblink t2 
     where t1.cust_id=t2.cust_id)

回答by Jonathan Leffler

It depends on what you're going to use it for, but I'd be sorely tempted to use a view instead of a table:

这取决于你打算用它做什么,但我非常想使用视图而不是表格:

create view temp1(cust_id, invoice_amt, payment_date) as
    select t1.cust_id, t1.invoice_amt, t2.payment_date 
      from table_1@dblink as t1 inner join table_2@dblink as t2
           on t1.cust_id = t2.cust_id

The advantage is it always contains the values from the current versions of table_1 and table_2. The disadvantage is that you cannot edit the view (or, if you can, your edits affect the underlying tables as well as the view).

优点是它始终包含来自 table_1 和 table_2 的当前版本的值。缺点是您无法编辑视图(或者,如果可以,您的编辑会影响基础表和视图)。