【问题标题】:combining and joining two tables with different no. of columns and same column name组合和连接两个不同编号的表。列和相同的列名
【发布时间】:2008-12-06 09:35:09
【问题描述】:

我尝试将 fus_shift 和根表 的表合并到一个新的 final table 表中,但它在第 2 行输出类似于“ERROR: ORA-01789: 查询块的结果列数不正确”。我也尝试加入表作为我的替代方案,但它也输出“ERROR at line 3: ORA-00918: column ambiguously defined "。有没有另一种方法来组合和连接两个具有不同列数并分别具有相同列名的表?再次感谢:-)

代码错误:
创建表最终为
从 fus_shift 中选择 *
工会
从根目录中选择 *;

代码错误:
选择record_num,test_num,t_date,t_time,system_type,category,cmets,val
来自 fus_shiftrpt,root
其中record_num=record_num;

我的桌子:

                                 fus_shift Table


           Record_Num       test          date      time         system   
           -----------------------------------------------------------
                1          test15      08-11-12  13:20:01    sys23 
                2          test17      08-11-03  14:24:00    sys24
                3          test13      08-11-13  17:25:04    sys45
                4          test15      08-11-14  18:24:00    sys67
                5          test16      08-11-15  19:24:06    sys45


                                 root Table

           Record_Num      category   comments    validated by
           ---------------------------------------------------
                  1        dirt        checked        admin
                  2        prog        checked        admin
                  3        dirt        checked        pe
                  4        wires       checked        ee
                  5        prog        repair         admin

强调文字

【问题讨论】:

    标签: sql oracle sqlplus ora-00918


    【解决方案1】:

    您当然不能将“联合”应用于您的表。只有当两个查询都返回相同数量(并且类型相似)的列时,才能应用它。

    您可以连接两个表,但在连接时必须使用“表别名”,因为“record_num”字段在两个表中都很常见。这是适合您的查询

    select 
            table1.record_num,
            table1.test_num,
            table1.t_date,
            table1.t_time,
            table1.system_type,
            table2.category,
            table2.comments,
            table2.val
    from 
           fus_shift table1,root table2
    where 
           table1.record_num = table2.record_num;
    

    【讨论】:

    • 不需要使用表别名来解析查询; WHERE 子句很可能是 'WHERE fus_shift.record_num = root.record_num'。
    • 这也行。如果我们不想使用表别名,除了 WHERE 子句,我们还必须使用表名解析 SELECT 子句中的公共字段。
    【解决方案2】:

    我会使用以下方法:

    SELECT * 
    FROM fus_shift
    INNER JOIN root ON root.record_num = fus_shift.record_num
    

    【讨论】:

    • ON 的选项是 USING .... USING (record_num) 因为在这种情况下,两个表中的列名称都相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多