子查询的问题,子句提供多值,提示ORA-00913: 值过多??

matoude1234567 2009-10-21 10:37:10
查询是这样写的,但执行有错。

SQL> select ename,sal from emp where sal in (select max(sal),min(sal) from emp);
ERROR 位于第 1 行:
ORA-00913: 值过多

我觉得这样的句子没问题,因为下面这个句子就不会出错,上面那个只是一个子句而已,而且返回的值就是5000,和800.
SQL> select ename,sal from emp where sal in (5000,800);

困扰,求解
...全文
1383 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼W 2009-10-22
  • 打赏
  • 举报
回复
in可以用在(select max(sal) from emp union all select min(sal) from emp)上,是2条记录,而不是并列的2个字段.(5000,800)是2个值,不是一个值中的2个字段
可以改成
select ename,sal from emp a
where not exists(select 1 from emp where sal>a.sal)
or not exists(select 1 from emp where sal<a.sal);
matoude1234567 2009-10-22
  • 打赏
  • 举报
回复
谢谢 wildwave 的讲解,但这个帖子好像给不了分了,以后有机会给你加分。谢谢。
小灰狼W 2009-10-22
  • 打赏
  • 举报
回复
因为sal值为空值的时候不能进行<和>比较
可以加个条件
select ename,sal from emp a
where (not exists(select 1 from emp where sal>a.sal)
or not exists(select 1 from emp where sal <a.sal))
and sal is not null;
比较下执行计划,这么写效率可能会比写2个聚合的子查询高一点

至于什么结果是对的,这是由你决定的,语句是根据需求写出来的,符合需求的就是对的
matoude1234567 2009-10-22
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 wildwave 的回复:]
in可以用在(select max(sal) from emp union all select min(sal) from emp)上,是2条记录,而不是并列的2个字段.(5000,800)是2个值,不是一个值中的2个字段
可以改成
select ename,sal from emp a
where not exists(select 1 from emp where sal>a.sal)
    or not exists(select 1 from emp where sal <a.sal);

[/Quote]
我才开始学 。。。基本看不明白,而且帖子昨晚已经结了

不过我试了一下,如果表里面我加一个 sal 为空的人(Peter) ,那么结果会是:

ENAME SAL
---------- ----------
SMITH 800
KING 5000
Peter

也许你这样的结果才是对的?

这句的结果是:其中
SQL> select ename,sal from emp where sal =(select max(sal) from emp) or sal=(select min(sal) from emp);


ENAME SAL
---------- ----------
SMITH 800
KING 5000
sbfuksn775 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 taoyuan0908 的回复:]
用and的话,就意味着选择既是最大值又是最小值的值,这样不是矛盾吗?所以无值,提示未选定行~
[/Quote]

西西,,一失足成千古恨。。。。。。太大意了
matoude1234567 2009-10-21
  • 打赏
  • 举报
回复
谢谢 sbfuksn775 捧场
谢谢 taoyuan0908 帮忙
谢谢 meteor5118 参与

这个问题先这么着吧,说不定以后就慢慢不那么困惑了
sbfuksn775 2009-10-21
  • 打赏
  • 举报
回复
分,分吧
matoude1234567 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 taoyuan0908 的回复:]
select max(sal),min(sal) from emp查询出来的值有两列,where sal 只需要一列,所以值过多。
sal in (select 一列 from table)
[/Quote]

哦,那就不需要用 in 了 ,用个 = 就可以了。
当时考虑的就是max 和 min 会返回两列,所以才用的in 。
此问有解,但仍然困惑。
一方晴空 2009-10-21
  • 打赏
  • 举报
回复
用and的话,就意味着选择既是最大值又是最小值的值,这样不是矛盾吗?所以无值,提示未选定行~
一方晴空 2009-10-21
  • 打赏
  • 举报
回复
select max(sal),min(sal) from emp查询出来的值有两列,where sal 只需要一列,所以值过多。
sal in (select 一列 from table)
matoude1234567 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sbfuksn775 的回复:]
select ename,sal from emp where sal in (select max(sal) from emp) and sal in (select min(sal) from emp)
你这样写看下吧,西西,多少要给点 分的,继续关注。。
[/Quote]
你用的 and 不行诶, 提示 未选定行 。。。
一方晴空 2009-10-21
  • 打赏
  • 举报
回复
select ename,sal from emp where sal in (select min(sal) from emp) or sal in(select max(sal) from emp); 

上面那个忘了加括号。
matoude1234567 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 taoyuan0908 的回复:]
SQL codeselect ename,salfrom empwhere salin (selectmin(sal)from emp)or sal inselectmax(sal)from emp;
[/Quote]

这样可以诶~ 谢谢
多问一句,为什么 max 和min 写在一个子句里会报错呢?
matoude1234567 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sbfuksn775 的回复:]
select ename,sal from emp where sal in (select max(sal),min(sal) from emp);
兄弟你where后面要跟个确定的值,西西有两个当然会这样提示你了。
select ename,sal from emp where sal in (select max(sal)from emp);
[/Quote]

恩,一个 max(sal) 是没问题 ,这个我试了。
可是 select ename,sal from emp where sal in (5000,800); 有两个值也不会报错呀。
如果子句提供多值报错的话,万一有这种需求岂不很麻烦了
sbfuksn775 2009-10-21
  • 打赏
  • 举报
回复
select ename,sal from emp where sal in (select max(sal) from emp) and sal in (select min(sal) from emp)
你这样写看下吧,西西,多少要给点 分的,继续关注。。
一方晴空 2009-10-21
  • 打赏
  • 举报
回复
select ename,sal from emp where sal in (select min(sal) from emp) or sal inselect max(sal) from emp; 
sbfuksn775 2009-10-21
  • 打赏
  • 举报
回复
select ename,sal from emp where sal in (select max(sal),min(sal) from emp);
兄弟你where后面要跟个确定的值,西西有两个当然会这样提示你了。
select ename,sal from emp where sal in (select max(sal)from emp);
matoude1234567 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 matoude1234567 的回复:]
引用 1 楼 meteor5118 的回复:
有吗


稍等我把oracle重启一下,试试
[/Quote]

还是报 值过多 的错误。

仍然困惑,求解
matoude1234567 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 meteor5118 的回复:]
有吗
[/Quote]

稍等我把oracle重启一下,试试
auvKone 2009-10-21
  • 打赏
  • 举报
回复
有吗

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧