【问题标题】:ORDER BY and UNION in SQLPLUS issueSQLPLUS 问题中的 ORDER BY 和 UNION
【发布时间】:2018-10-28 18:52:43
【问题描述】:

我给出以下错误:“ORA-01785: ORDER BY item must be the number of a SELECT-list expression”当我尝试使用“order by”选择 id_prof 并在不同表中的列之间建立联合时.

命令如下:

select substr(nume, 1, 1)||'.'||regexp_replace(prenume, '[aeiou]', null, 1, 0, 'i') as "Rododendron" 
from studenti 
union 
select ceil(sqrt(to_number(substr (id_prof, 2, 1))*2)) as "Fata de con" 
from profesori 
where grup_didactic = 'Lect' 
order by to_number(substr (id_prof, 2, 1)) desc;

表格如下图:FirstPictureSecondPicture

我想最后有两列。

第一个应包含“nume”的第一个字母,并与 '.' 连接。和没有元音的“prenume”(名为“Rododendron”的列)。

在第二个中,我想从 id_prof 的数字的双倍(名为“Fata de con”的列)中获得平方根,用于“profesori”中可以找到的所有内容,其中 grup_didactic = 'lector ' 并且所有内容都在 id_prof 之后按 desc 排序。

有什么帮助吗?

【问题讨论】:

    标签: sql oracle union


    【解决方案1】:

    Oracle 说您不能按SELECT 列列表中未包含的列对结果进行排序;例如在这个简单的 Scott 模式示例中:

    SQL> select ename from emp
      2  union
      3  select dname from dept
      4  order by substr(ename, 1, 3);
    order by substr(ename, 1, 3)
             *
    ERROR at line 4:
    ORA-01785: ORDER BY item must be the number of a SELECT-list expression
    

    我想说 排序 是目前您遇到的最少的问题。 UNION 不会返回两列;删除 ORDER BY 并亲自查看 - 如果您幸运,您将获得一列。

    这就是你现在要做的:

    select col from studenti
    union
    select another_col from profesori
    

    当你想得到时

    select col, another_col
    from ...
    

    FROM 子句很棘手,因为您似乎从两个不同的表中进行选择。你能加入他们,这样查询看起来像第二个选项吗?如果不是,你总是可以做笛卡尔积,但这很可能是一个错误的结果。

    如果您提供示例输入数据并说明如何从该输入中获取结果,将会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 2010-10-04
      • 1970-01-01
      • 2011-09-06
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2016-11-22
      相关资源
      最近更新 更多