我有错误1242子查询返回超过1行

时间:2013-10-16 08:34:33

标签: mysql sql

select (select Nombre 
        from Pacientes 
        where idPacientes= any(select idPaciente 
                                from Citas 
                                where Dias_idDia= any(select idDia 
                                                      from Dias 
                                                      where fecha = '2013-10-15'))) as 'Nombre',horaInicio, horaTermino,actividad,observacion,recordar,ciudad,tipoCita 
from Citas 
where Dias_idDia = any (select idDia 
                        from Dias 
                        where fecha='2013-10-15')
order by horaInicio;

我有错误1242,如果有人可以帮忙解决这个问题,因为它在我的系统中给了我很多麻烦。 TY

2 个答案:

答案 0 :(得分:4)

子查询中的行数不正确:

错误1242(ER_SUBSELECT_NO_1_ROW) SQLSTATE = 21000 Message =“子查询返回超过1行” 对于子查询必须最多返回一行但返回多行的语句,会发生此错误。请考虑以下示例:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

如果SELECT column1 FROM t2只返回一行,则前一个查询将起作用。如果子查询返回多行,则会发生错误1242。在这种情况下,查询应该重写为:

SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);

<强> Reference

答案 1 :(得分:2)

   select (select Nombre 
       from Pacientes 
       where idPacientes in (select idPaciente 
                            from Citas 
                            where Dias_idDia in (select idDia 
                                                  from Dias 
                                                   where fecha = '2013-10-15'))) as
  'Nombre',horaInicio, horaTermino,actividad,observacion,recordar,ciudad,tipoCita 
   from Citas 
   where Dias_idDia in (select idDia 
                    from Dias 
                    where fecha='2013-10-15')
   order by horaInicio;