单行子查询返回多行

he1hehe 2012-02-13 09:20:01
今天这个sql语句报了一个单行子查询返回多行的错误

select t.*,
(select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1) as weekevolve
from T_MK_PLAN t
where t.t_plan_submitflag <> 0
and t.t_plan_week = 4
and t.t_plan_deptid = 48000147100
and t.T_PLAN_YEAR = 2011
and t.t_sys_flag <> -1
order by t.t_plan_id desc

从网上查了一下,看到一个例子
select 名字,姓氏 from 雇员 where 薪金 = (select 薪金 from 雇员 where 部门标识 =20);
这个例子看懂了,但是上面那个是怎么回事?求高手指教。
...全文
508 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
he1hehe 2012-02-20
  • 打赏
  • 举报
回复
哦,明白了,是数据有问题。
he1hehe 2012-02-16
  • 打赏
  • 举报
回复
不明白,把t.t_plan_week = 4换成t.t_plan_week = 6就没问题了

select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1

这个子查询也是返回多行,但是整个sql就能执行了,能查询出结果了。
he1hehe 2012-02-16
  • 打赏
  • 举报
回复
不明白,把t.t_plan_week = 4换成t.t_plan_week = 6就没问题了?
争取今天 2012-02-16
  • 打赏
  • 举报
回复
这种情况有可能是数据问题,除了异常.先检查数据是否合法.

如果合法,只能修改语句了
  • 打赏
  • 举报
回复
能上几行数据看看么
宋哥 2012-02-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 travylee 的回复:]
select t.*,--查询 T_MK_PLAN 表(别名t)里的所有字段
(select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1) as weekevolve--把这个查询……
[/Quote]
这个注解很好,可在子查询中加条件限制,保证只有一行,就不会报错了,例如:

select t.*,
(select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1 and rownum=1 ) as weekevolve
from T_MK_PLAN t
where t.t_plan_submitflag <> 0
and t.t_plan_week = 4
and t.t_plan_deptid = 48000147100
and t.T_PLAN_YEAR = 2011
and t.t_sys_flag <> -1
order by t.t_plan_id desc
Rotel-刘志东 2012-02-14
  • 打赏
  • 举报
回复
具体要把条件限制一下了。
  • 打赏
  • 举报
回复
select t.*,--查询 T_MK_PLAN 表(别名t)里的所有字段
(select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1) as weekevolve--把这个查询语句的结果作为一个新的列,取名weekevolve
from T_MK_PLAN t
where t.t_plan_submitflag <> 0
and t.t_plan_week = 4
and t.t_plan_deptid = 48000147100
and t.T_PLAN_YEAR = 2011
and t.t_sys_flag <> -1
order by t.t_plan_id desc
  • 打赏
  • 举报
回复
select t.*,
(select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1) as weekevolve
--把这个查询语句的结果作为一个新的列,取名weekevolve
from T_MK_PLAN t
where t.t_plan_submitflag <> 0
and t.t_plan_week = 4
and t.t_plan_deptid = 48000147100
and t.T_PLAN_YEAR = 2011
and t.t_sys_flag <> -1
order by t.t_plan_id desc
我心飞翔 2012-02-14
  • 打赏
  • 举报
回复
(select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1)

这个子查询返回了多个结果(即有多条记录满足当前的查询条件),就会出现上述错误。
如果该返回结果,只有一条记录,则不会出现错误。
不要悲剧人生 2012-02-13
  • 打赏
  • 举报
回复
根据你的条件T_MK_PLAN 表中的一条数据,在 t_mk_mission表中有多条数据对应。
youshang444 2012-02-13
  • 打赏
  • 举报
回复
select t_evolve_weekevolve
from t_mk_mission
where t_evolve_id = t.t_plan_id
and t_evolve_week = t.t_plan_week
and t_sys_flag <> -1
这一条语句返回了多条记录

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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