Error (single-row subquery returns more than one row)

孤街浪徒 提交于 2021-02-17 06:54:06

问题


What is wrong with this query?

select en.*,
       (select sf.red, sf.blue, sf.green, sf.yellow from data2 sf )
from data1 en;

I have error

ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"


回答1:


This is your query:

select en.*,
       (select sf.red, sf.blue, sf.green, sf.yellow from data2 sf )
from data1 en;

A subquery in the select used like this is called a scalar subquery. Such a subquery can be used where a scalar value (i.e. single value such as a number or string) can be used.

Scalar subqueries must meet two conditions:

  • It returns at most one row.
  • It returns one column.

The solution in your case is simple, using a LEFT JOIN:

select en.*, sf.red, sf.blue, sf.green, sf.yellow 
from data1 en left join
     data2 sf
     on 1=1;

Or, in Oracle 12C, you can use a lateral join:

select en.*, sf.red, sf.blue, sf.green, sf.yellow 
from data1 en left join lateral
     data2 sf
     on 1=1;

The two look the same in this case, but a lateral join can also be used for a correlated subquery.



来源:https://stackoverflow.com/questions/57850905/error-single-row-subquery-returns-more-than-one-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!