今天这个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);
这个例子看懂了,但是上面那个是怎么回事?求高手指教。

解决方案 »

  1.   

    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这一条语句返回了多条记录
      

  2.   

    根据你的条件T_MK_PLAN 表中的一条数据,在 t_mk_mission表中有多条数据对应。
      

  3.   

    (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)这个子查询返回了多个结果(即有多条记录满足当前的查询条件),就会出现上述错误。
    如果该返回结果,只有一条记录,则不会出现错误。
      

  4.   

    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
      

  5.   

    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
      

  6.   

    这个注解很好,可在子查询中加条件限制,保证只有一行,就不会报错了,例如: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
      

  7.   

    不明白,把t.t_plan_week = 4换成t.t_plan_week = 6就没问题了?
      

  8.   

    不明白,把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就能执行了,能查询出结果了。